From 137f9fbd66f243a2f1a946877db84b600341f0c7 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Thu, 30 May 2024 07:36:00 +0530 Subject: [PATCH] Added codes for 29 and 30 May --- GeeksForGeeks/May/29-5-24/GFG.java | 52 ++++++++++++++++++++++++++ GeeksForGeeks/May/29-5-24/README.md | 2 + GeeksForGeeks/May/30-5-24/GFG.java | 58 +++++++++++++++++++++++++++++ GeeksForGeeks/May/30-5-24/README.md | 2 + LeetCode/May/29-5-24/README.md | 2 + LeetCode/May/29-5-24/Solution.java | 27 ++++++++++++++ LeetCode/May/30-5-24/README.md | 2 + LeetCode/May/30-5-24/Solution.java | 20 ++++++++++ 8 files changed, 165 insertions(+) create mode 100644 GeeksForGeeks/May/29-5-24/GFG.java create mode 100644 GeeksForGeeks/May/29-5-24/README.md create mode 100644 GeeksForGeeks/May/30-5-24/GFG.java create mode 100644 GeeksForGeeks/May/30-5-24/README.md create mode 100644 LeetCode/May/29-5-24/README.md create mode 100644 LeetCode/May/29-5-24/Solution.java create mode 100644 LeetCode/May/30-5-24/README.md create mode 100644 LeetCode/May/30-5-24/Solution.java diff --git a/GeeksForGeeks/May/29-5-24/GFG.java b/GeeksForGeeks/May/29-5-24/GFG.java new file mode 100644 index 0000000..38b5fab --- /dev/null +++ b/GeeksForGeeks/May/29-5-24/GFG.java @@ -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=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; + } +} diff --git a/GeeksForGeeks/May/29-5-24/README.md b/GeeksForGeeks/May/29-5-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/GeeksForGeeks/May/29-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/GeeksForGeeks/May/30-5-24/GFG.java b/GeeksForGeeks/May/30-5-24/GFG.java new file mode 100644 index 0000000..e0d162c --- /dev/null +++ b/GeeksForGeeks/May/30-5-24/GFG.java @@ -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]; + } +} diff --git a/GeeksForGeeks/May/30-5-24/README.md b/GeeksForGeeks/May/30-5-24/README.md new file mode 100644 index 0000000..8faf6d9 --- /dev/null +++ b/GeeksForGeeks/May/30-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*m) +Space complexity - O(n*m) diff --git a/LeetCode/May/29-5-24/README.md b/LeetCode/May/29-5-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/LeetCode/May/29-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/May/29-5-24/Solution.java b/LeetCode/May/29-5-24/Solution.java new file mode 100644 index 0000000..0b69d33 --- /dev/null +++ b/LeetCode/May/29-5-24/Solution.java @@ -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; + } +} diff --git a/LeetCode/May/30-5-24/README.md b/LeetCode/May/30-5-24/README.md new file mode 100644 index 0000000..1d3ee04 --- /dev/null +++ b/LeetCode/May/30-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n^2) +Space complexity - O(1) diff --git a/LeetCode/May/30-5-24/Solution.java b/LeetCode/May/30-5-24/Solution.java new file mode 100644 index 0000000..208e038 --- /dev/null +++ b/LeetCode/May/30-5-24/Solution.java @@ -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; + } +}