From 050c383c1f91094fcbaf56791f9cb583b9ab7d80 Mon Sep 17 00:00:00 2001 From: YOGESH CHAND UPADHYAY <84492118+yogesh-9999@users.noreply.github.com> Date: Thu, 13 Oct 2022 23:26:56 +0530 Subject: [PATCH 1/2] LongestpalindromicSubstring in Java and python --- .../solutionInJava.java | 36 +++++++++++++++++++ .../solutioninpython.py | 27 ++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Leetcode-Medium/5-LongestPalindromicSubstring/solutionInJava.java create mode 100644 Leetcode-Medium/5-LongestPalindromicSubstring/solutioninpython.py diff --git a/Leetcode-Medium/5-LongestPalindromicSubstring/solutionInJava.java b/Leetcode-Medium/5-LongestPalindromicSubstring/solutionInJava.java new file mode 100644 index 0000000..08d4473 --- /dev/null +++ b/Leetcode-Medium/5-LongestPalindromicSubstring/solutionInJava.java @@ -0,0 +1,36 @@ +class Solution { + public String longestPalindrome(String s) { + char[] sChars = s.toCharArray(); + + int m = s.length(); + + // * dp[i][len + 1] means substring starting from i with length of len; + boolean[][] dp = new boolean[m][2]; + int currCol = 0; + + int maxLen = 0; + int ans = 0; // record start index of s + + for (int len = 0; len < m; len++) { + for (int start = 0; start + len < m; start++) { + int end = start + len; + if (len == 0) { + dp[start][currCol] = true; + } else if (len == 1) { + dp[start][currCol] = (sChars[start] == sChars[end]); + } else { + dp[start][currCol] = (sChars[start] == sChars[end] && dp[start + 1][currCol]); + } + + if (dp[start][currCol] && len + 1 > maxLen) { + ans = start; + maxLen = len + 1; + } + } + + currCol = 1 - currCol; + } + + return maxLen == 0 ? "" : s.substring(ans, ans + maxLen); + } +} \ No newline at end of file diff --git a/Leetcode-Medium/5-LongestPalindromicSubstring/solutioninpython.py b/Leetcode-Medium/5-LongestPalindromicSubstring/solutioninpython.py new file mode 100644 index 0000000..7211d13 --- /dev/null +++ b/Leetcode-Medium/5-LongestPalindromicSubstring/solutioninpython.py @@ -0,0 +1,27 @@ +class Solution: + def longestPalindrome(self, s: str) -> str: + + n = len(s) + # Form a bottom-up dp 2d array + # dp[i][j] will be 'true' if the string from index i to j is a palindrome. + dp = [[False] * n for _ in range(n)] + + ans = '' + # every string with one character is a palindrome + for i in range(n): + dp[i][i] = True + ans = s[i] + + maxLen = 1 + for start in range(n-1, -1, -1): + for end in range(start+1, n): + # palindrome condition + if s[start] == s[end]: + # if it's a two char. string or if the remaining string is a palindrome too + if end - start == 1 or dp[start+1][end-1]: + dp[start][end] = True + if maxLen < end - start + 1: + maxLen = end - start + 1 + ans = s[start: end+ 1] + + return ans \ No newline at end of file From 8e9ca110a3bf8ad5a8c75cbdfc31bb0e57e56a4a Mon Sep 17 00:00:00 2001 From: YOGESH CHAND UPADHYAY <84492118+yogesh-9999@users.noreply.github.com> Date: Thu, 13 Oct 2022 23:32:19 +0530 Subject: [PATCH 2/2] added Java solution for validtae stack sequences in Java --- .../946-validate-stack-sequences/solution.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Leetcode-Medium/946-validate-stack-sequences/solution.java diff --git a/Leetcode-Medium/946-validate-stack-sequences/solution.java b/Leetcode-Medium/946-validate-stack-sequences/solution.java new file mode 100644 index 0000000..484b1ab --- /dev/null +++ b/Leetcode-Medium/946-validate-stack-sequences/solution.java @@ -0,0 +1,12 @@ +public boolean validateStackSequences(int[] pushed, int[] popped) { + Stack stack = new Stack<>(); + int i = 0; + for (int x : pushed) { + stack.push(x); + while (!stack.empty() && stack.peek() == popped[i]) { + stack.pop(); + i++; + } + } + return stack.empty(); +} \ No newline at end of file