Skip to content

Commit

Permalink
Added codes for 21 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 21, 2024
1 parent 2e0642c commit b158f73
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
84 changes: 84 additions & 0 deletions GeeksForGeeks/February/21-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//{ Driver Code Starts
//Initial Template for Java

import java.io.*;
import java.util.*;

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

Solution ob = new Solution();
System.out.println(ob.countWays(N, S));
}
}
}
// } Driver Code Ends


//User function Template for Java

class Solution
{
static int countWays(int n, String s)
{
int mod = 1003;
char[] symbols = s.toCharArray();
int[][][] dp = new int[n][n][2];

for (int i = 0; i < n; i += 2)
{
if (symbols[i] == 'T')
{
dp[i][i][1] = 1;
}
else
{
dp[i][i][0] = 1;
}
}

for (int len = 2; len < n; len += 2)
{
for (int i = 0; i < n - len; i += 2)
{
int j = i + len;
for (int k = i + 1; k < j; k += 2)
{
char operator = symbols[k];
if (operator == '&')
{
dp[i][j][1] = (dp[i][j][1] + (dp[i][k - 1][1] * dp[k + 1][j][1]) % mod) % mod;

dp[i][j][0] = (dp[i][j][0] + ((dp[i][k - 1][0] * dp[k + 1][j][0]) % mod
+ (dp[i][k - 1][0] * dp[k + 1][j][1]) % mod
+ (dp[i][k - 1][1] * dp[k + 1][j][0]) % mod) % mod) % mod;
}
else if (operator == '|')
{
dp[i][j][0] = (dp[i][j][0] + (dp[i][k - 1][0] * dp[k + 1][j][0]) % mod) % mod;

dp[i][j][1] = (dp[i][j][1] + ((dp[i][k - 1][1] * dp[k + 1][j][1]) % mod
+ (dp[i][k - 1][0] * dp[k + 1][j][1]) % mod
+ (dp[i][k - 1][1] * dp[k + 1][j][0]) % mod) % mod) % mod;
}
else if (operator == '^')
{
dp[i][j][1] = (dp[i][j][1] + ((dp[i][k - 1][1] * dp[k + 1][j][0]) % mod
+ (dp[i][k - 1][0] * dp[k + 1][j][1]) % mod) % mod) % mod;

dp[i][j][0] = (dp[i][j][0] + ((dp[i][k - 1][1] * dp[k + 1][j][1]) % mod
+ (dp[i][k - 1][0] * dp[k + 1][j][0]) % mod) % mod) % mod;
}
}
}
}

return dp[0][n - 1][1];
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/21-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^3)
Space complexity - O(n^2)
2 changes: 2 additions & 0 deletions LeetCode/February/21-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(1)
Space complexity - O(1)
16 changes: 16 additions & 0 deletions LeetCode/February/21-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution
{
public int rangeBitwiseAnd(int left, int right)
{
int shiftBits = 0;

while (left != right)
{
left >>= 1;
right >>= 1;
++shiftBits;
}

return left << shiftBits;
}
}

0 comments on commit b158f73

Please sign in to comment.