Skip to content

Commit

Permalink
Added codes for 29 and 30 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 30, 2024
1 parent 345281e commit 137f9fb
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 0 deletions.
52 changes: 52 additions & 0 deletions GeeksForGeeks/May/29-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while (t-- > 0) {

int n;
n = Integer.parseInt(br.readLine());

int x;
x = Integer.parseInt(br.readLine());

int y;
y = Integer.parseInt(br.readLine());

Solution obj = new Solution();
int res = obj.findWinner(n, x, y);

System.out.println(res);
}
}
}

// } Driver Code Ends



class Solution {
public static int findWinner(int n, int x, int y) {
// code here
int []dp= new int[n+1];
dp[1]=1;
for(int i=2;i<dp.length;i++)
{
if(dp[i-1]==0)
dp[i]=1;
else if(i-x>=0 && dp[i-x]==0)
dp[i]=1;
else if(i-y>=0 && dp[i-y]==0)
dp[i]=1;
else
dp[i]=0;
}

return dp[n]==1?1:0;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/29-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
58 changes: 58 additions & 0 deletions GeeksForGeeks/May/30-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while (t-- > 0) {

String s1;
s1 = br.readLine();

String s2;
s2 = br.readLine();

Solution obj = new Solution();
int res = obj.countWays(s1, s2);

System.out.println(res);
}
}
}

// } Driver Code Ends



class Solution {
public static int countWays(String s1, String s2) {
// code here
int MOD = 1000000007;
int n = s1.length();
int m = s2.length();
int[][] dp = new int[n + 1][m + 1];

for (int i = 0; i <= n; i++)
dp[i][0] = 1;

for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (s1.charAt(i - 1) == s2.charAt(j - 1))
{
dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j]) % MOD;
}
else
{
dp[i][j] = dp[i - 1][j] % MOD;
}
}
}

return dp[n][m];
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/30-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*m)
Space complexity - O(n*m)
2 changes: 2 additions & 0 deletions LeetCode/May/29-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
27 changes: 27 additions & 0 deletions LeetCode/May/29-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution
{
public int numSteps(String s)
{
int ans = 0;
StringBuilder sb = new StringBuilder(s);

while (sb.charAt(sb.length() - 1) == '0')
{
sb.deleteCharAt(sb.length() - 1);
++ans;
}

if (sb.toString().equals("1"))
return ans;

// `s` is now odd, so add 1 to `s` and cost 1 step.
++ans;

// All the 1s will become 0s and can be popped by 1 step.
// All the 0s will become 1s and can be popped by 2 steps (adding 1 then dividing by 2).
for (char c : sb.toString().toCharArray())
ans += c == '1' ? 1 : 2;

return ans;
}
}
2 changes: 2 additions & 0 deletions LeetCode/May/30-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(1)
20 changes: 20 additions & 0 deletions LeetCode/May/30-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution
{
public int countTriplets(int[] arr)
{
int count = 0;

for(int i = 0; i < arr.length; i++)
{
int xor = 0;
for(int j = i; j < arr.length; j++)
{
xor ^= arr[j];
if(xor == 0)
count += (j - i);
}
}

return count;
}
}

0 comments on commit 137f9fb

Please sign in to comment.