From 3f4881e5d39aeafed1170d2ec583a13cfa6734f1 Mon Sep 17 00:00:00 2001 From: saisuryajss Date: Sun, 9 Oct 2022 21:05:52 +0530 Subject: [PATCH 1/3] Longest Valid Parenthesis --- .../LongestValidParentheses.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Hacktoberfest2022/LongestValidParentheses.java diff --git a/Hacktoberfest2022/LongestValidParentheses.java b/Hacktoberfest2022/LongestValidParentheses.java new file mode 100644 index 0000000..c061938 --- /dev/null +++ b/Hacktoberfest2022/LongestValidParentheses.java @@ -0,0 +1,100 @@ +import java.util.*; + +public class LongestValidParentheses { + + public static void main(String[] args) { + String str=")()())"; + System.out.println(longestValidParenthesesRecursion(str)); + System.out.println(longestValidParentheses1D(str)); + System.out.println(longestValidParentheses(str)); + } + + //using recursion + //Time complexity: O(n^3) + //Space complexity: O(n) + public static boolean isValid(String str) { + Stack stack = new Stack(); + int n=str.length(); + //check if parantheses are balanced or not + for (int i = 0; i < n; i++) { + if (str.charAt(i) == '(') { + stack.push('('); + } else if (!stack.empty() && stack.peek() == '(') { + stack.pop(); + } else { + return false; + } + } + return stack.empty(); + } + + public static int longestValidParenthesesRecursion(String str) { + int ans = 0; + int n=str.length(); + for (int i = 0; i < str.length(); i++) { + for (int j = i + 2; j <= str.length(); j+=2) { + //checking if every substring from i to j-1 is valid or not + if (isValid(str.substring(i, j))) { + ans = Math.max(ans, j - i); + } + } + } + return ans; + } + + //using dynamic programming + //Time complexity: O(n) + //Space complexity: O(n) + public static int longestValidParentheses1D(String str) { + int n=str.length(); + int ans=0; + int []dp=new int[n]; + for(int i=1;i=0?dp[i-2]+2:2; + } + else{ + if(i-dp[i-1]-1>=0&&str.charAt(i-dp[i-1]-1)=='('){ + dp[i]=dp[i-1]+2+((i-dp[i-1]-2)>=0?dp[i-dp[i-1]-2]:0); + } + } + } + ans=Math.max(ans,dp[i]); + } + return ans; + } + + //using dynamic programming + //Time complexity:O(n) + //space complexity:O(1) + public static int longestValidParentheses(String str) { + int left = 0, right = 0, ans = 0; + for (int i = 0; i < str.length(); i++) { + if (str.charAt(i) == '(') { + left++; + } else { + right++; + } + if (left == right) { + ans = Math.max(ans, 2 * right); + } else if (right >= left) { + left = right = 0; + } + } + left = right = 0; + for (int i = str.length() - 1; i >= 0; i--) { + if (str.charAt(i) == '(') { + left++; + } else { + right++; + } + if (left == right) { + ans = Math.max(ans, 2 * left); + } else if (left >= right) { + left = right = 0; + } + } + return ans; + } +} From 2d2d31c7b4bce8cddcffdcf480246ee355216b26 Mon Sep 17 00:00:00 2001 From: saisuryajss Date: Sun, 9 Oct 2022 21:07:56 +0530 Subject: [PATCH 2/3] Valid Sudoku --- Hacktoberfest2022/ValidSudoku.java | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Hacktoberfest2022/ValidSudoku.java diff --git a/Hacktoberfest2022/ValidSudoku.java b/Hacktoberfest2022/ValidSudoku.java new file mode 100644 index 0000000..e99b193 --- /dev/null +++ b/Hacktoberfest2022/ValidSudoku.java @@ -0,0 +1,55 @@ +import java.util.*; + +public class ValidSudoku{ + + public static void main(String []args){ + char [][]sudoku=new char[][]{{'8','3','.','.','7','.','.','.','.'}, + {'6','.','.','1','9','5','.','.','.'}, + {'.','9','8','.','.','.','.','6','.'}, + {'8','.','.','.','6','.','.','.','3'}, + {'4','.','.','8','.','3','.','.','1'}, + {'7','.','.','.','2','.','.','.','6'}, + {'.','6','.','.','.','.','2','8','.'}, + {'.','.','.','4','1','9','.','.','5'}, + {'.','.','.','.','8','.','.','7','9'}}; + System.out.println(isValidSudoku(sudoku)); + } + + //using bitmasking + public static boolean isValidSudoku(char[][] board) { + //arrays for marking numbers as visited for row,column,block + int brow[]=new int[9]; + int bcol[]=new int[9]; + int bblock[]=new int[9]; + for(int i=0;i<9;i++){ + for(int j=0;j<9;j++){ + //if empty slot is encountered continue + if(board[i][j]=='.'){ + continue; + } + int val=(int)Math.pow(2,board[i][j]-'0'); + //check if the value is already encountered in the row + if((brow[i]&val)>0){ + return false; + } + //if not encountered, mark it as encountered + brow[i]+=val; + //check if the value is already encountered in the column + if((bcol[j]&val)>0){ + return false; + } + //if not encountered, mark it as encountered + bcol[j]+=val; + int mod = (3 * (i / 3)) + (j / 3); + //check if the value is already encountered in the block + if((bblock[mod]&val)>0){ + return false; + } + //if not encountered, mark it as encountered + bblock[mod]+=val; + } + } + return true; + } + +} \ No newline at end of file From 7b079bb8a6c80bd624e86d501719dc660e4a14b1 Mon Sep 17 00:00:00 2001 From: saisuryajss Date: Sun, 9 Oct 2022 21:09:03 +0530 Subject: [PATCH 3/3] added myself --- Hacktoberfest2022/ValidSudoku.java | 55 ------------------------------ 1 file changed, 55 deletions(-) delete mode 100644 Hacktoberfest2022/ValidSudoku.java diff --git a/Hacktoberfest2022/ValidSudoku.java b/Hacktoberfest2022/ValidSudoku.java deleted file mode 100644 index e99b193..0000000 --- a/Hacktoberfest2022/ValidSudoku.java +++ /dev/null @@ -1,55 +0,0 @@ -import java.util.*; - -public class ValidSudoku{ - - public static void main(String []args){ - char [][]sudoku=new char[][]{{'8','3','.','.','7','.','.','.','.'}, - {'6','.','.','1','9','5','.','.','.'}, - {'.','9','8','.','.','.','.','6','.'}, - {'8','.','.','.','6','.','.','.','3'}, - {'4','.','.','8','.','3','.','.','1'}, - {'7','.','.','.','2','.','.','.','6'}, - {'.','6','.','.','.','.','2','8','.'}, - {'.','.','.','4','1','9','.','.','5'}, - {'.','.','.','.','8','.','.','7','9'}}; - System.out.println(isValidSudoku(sudoku)); - } - - //using bitmasking - public static boolean isValidSudoku(char[][] board) { - //arrays for marking numbers as visited for row,column,block - int brow[]=new int[9]; - int bcol[]=new int[9]; - int bblock[]=new int[9]; - for(int i=0;i<9;i++){ - for(int j=0;j<9;j++){ - //if empty slot is encountered continue - if(board[i][j]=='.'){ - continue; - } - int val=(int)Math.pow(2,board[i][j]-'0'); - //check if the value is already encountered in the row - if((brow[i]&val)>0){ - return false; - } - //if not encountered, mark it as encountered - brow[i]+=val; - //check if the value is already encountered in the column - if((bcol[j]&val)>0){ - return false; - } - //if not encountered, mark it as encountered - bcol[j]+=val; - int mod = (3 * (i / 3)) + (j / 3); - //check if the value is already encountered in the block - if((bblock[mod]&val)>0){ - return false; - } - //if not encountered, mark it as encountered - bblock[mod]+=val; - } - } - return true; - } - -} \ No newline at end of file