Skip to content

Commit 3a248cc

Browse files
committed
Update longest palindromic substring
1 parent 1aaab60 commit 3a248cc

File tree

7 files changed

+216
-75
lines changed

7 files changed

+216
-75
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ List of Programs related to data structures and algorithms
9292

9393
6. Valid palindrome: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/6.validPalindrome/validPalindrome.js) [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/6.validPalindrome/validPalindrome.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/6.validPalindrome/validPalindrome.md)
9494

95-
7. Longest palindromic substring: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/longestPalindromicSubstring.js)
95+
7. Longest palindromic substring: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/7.longestPalindromicSubstring/longestPalindromicSubstring.js) [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/7.longestPalindromicSubstring/longestPalindromicSubstring.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/7.longestPalindromicSubstring/longestPalindromicSubstring.md)
9696

9797
8. Palindromic substrings: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/palindromicStrings.js)
9898

src/java1/algorithms/strings/LongestPalindromicSubstring.java

-40
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//Expanding around center:- TC:O(n) SC:O(n)
2+
package java1.algorithms.strings.longestPalindromicSubstring;
3+
4+
public class LongestPalindromicSubstring {
5+
6+
private static String longestPalindromicSubstring1(String str) {
7+
if(str.length() <= 1) return str;
8+
9+
String maxPalindromicSubstr = str.substring(0, 1);
10+
11+
for (int i = 0; i < str.length(); i++) {
12+
String maxOddPalindromicSubstr = expandAroundCenter(str, i, i);
13+
String maxEvenPalindromicSubstr = expandAroundCenter(str, i, i+1);
14+
15+
if(maxOddPalindromicSubstr.length() > maxPalindromicSubstr.length()) {
16+
maxPalindromicSubstr = maxOddPalindromicSubstr;
17+
}
18+
if(maxEvenPalindromicSubstr.length() > maxPalindromicSubstr.length()) {
19+
maxPalindromicSubstr = maxEvenPalindromicSubstr;
20+
}
21+
}
22+
23+
return maxPalindromicSubstr;
24+
}
25+
26+
private static String expandAroundCenter(String str, int left, int right) {
27+
while(left >=0 && right < str.length() && str.charAt(left) == str.charAt(right)) {
28+
left--;
29+
right++;
30+
}
31+
return str.substring(left+1, right);
32+
}
33+
34+
private static String longestPalindromicSubstring2(String str){
35+
String longestSubstr = "";
36+
int longestSubstrLen = 0;
37+
int left =0, right = 0;
38+
39+
for(int i=0; i< str.length(); i++) {
40+
// Odd length
41+
left = i; right = i;
42+
while(left >=0 && right < str.length() && str.charAt(left) == str.charAt(right)) {
43+
if(right-left+1 > longestSubstrLen) {
44+
longestSubstr = str.substring(left, right+1);
45+
longestSubstrLen = right-left+1;
46+
}
47+
left--;
48+
right++;
49+
}
50+
// Even length
51+
left = i; right = i+1;
52+
while(left >=0 && right < str.length() && str.charAt(left) == str.charAt(right)) {
53+
if(right-left+1 > longestSubstrLen) {
54+
longestSubstr = str.substring(left, right+1);
55+
longestSubstrLen = right-left+1;
56+
}
57+
left--;
58+
right++;
59+
}
60+
}
61+
return longestSubstr;
62+
}
63+
64+
public static void main(String[] args) {
65+
String str1 = "ababd";
66+
String str2 = "dbbc";
67+
System.out.println(longestPalindromicSubstring1(str1));
68+
System.out.println(longestPalindromicSubstring1(str2));
69+
System.out.println(longestPalindromicSubstring2(str1));
70+
System.out.println(longestPalindromicSubstring2(str2));
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
**Description:**
2+
Given a string `str`, return the longest palindromic substring of `str`.
3+
4+
**Note:**A palindrome is a string that reads the same backward as forward. If the characters includes non-alphanumerical letters, they needs to be removed and uppercase letters should be converted in a lowercase format. If there are multiple palindromic substrings that have the same length, return any one of them.
5+
6+
### Examples
7+
Example 1:
8+
Input: s = "ababd"
9+
Output: "aba"
10+
11+
Example 2:
12+
Input: s = "dbbc"
13+
Output: "bb"
14+
15+
**Algorithmic Steps**
16+
This problem is solved with the help of **expanding around center** approach where we expand the input string at each position. The algorithmic approach can be summarized as follows:
17+
18+
1. Add a preliminary check by returning `str` if the length of the string is less than or equal to 1.
19+
20+
2. Initialize longest palindromic substring(`maxPalindromicSubstr`) to first character of given string.
21+
22+
3. Iterate over an input string until the end of string.
23+
24+
4. Create a method(`expandAroundCenter`) to expand the string from center for each character position. This function iterates over the string based on left and right pointers. The left pointer will be decremented and right pointer will be incremented when the characters are equals from both the ends and also both pointers exists with in the range of string boundaries.
25+
26+
5. Invoke the method created in the previous step to find both odd(`left = right = i`) and even(`left = i`, `right = i+1`) longest palindromes.
27+
28+
6. Update the longest palindrome by comparing the current longest palindrome with odd and even palindromes.
29+
30+
7. Repeat steps 4-6 until all the characters traversed within given input string.
31+
32+
8. Return longest palindromic substring(`maxPalindromicSubstr`) from the main function.
33+
34+
**Time and Space complexity:**
35+
This algorithm has a time complexity of `O(n*2)` because expanding a palindrome around its center could take `O(n)` and it needs to repeated for each character(i.e, `n` times).
36+
37+
Also, it doesn't requires any additional data structure. So the space complexity is `O(1)`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//Expanding around center:- TC:O(n) SC:O(n)
2+
3+
function longestPalindromicSubstring1(str) {
4+
if(str.length <= 1) return str;
5+
6+
let maxPalindromicSubstr = str.substring(0, 1);
7+
8+
for (let i = 0; i < str.length; i++) {
9+
let maxOddPalindromicSubstr = expandAroundCenter(str, i, i);
10+
let maxEvenPalindromicSubstr = expandAroundCenter(str, i, i+1);
11+
12+
if(maxOddPalindromicSubstr.length > maxPalindromicSubstr.length) {
13+
maxPalindromicSubstr = maxOddPalindromicSubstr;
14+
}
15+
if(maxEvenPalindromicSubstr.length > maxPalindromicSubstr.length) {
16+
maxPalindromicSubstr = maxEvenPalindromicSubstr;
17+
}
18+
}
19+
20+
return maxPalindromicSubstr;
21+
}
22+
23+
function expandAroundCenter(str, left, right) {
24+
while(left >=0 && right < str.length && str[left] === str[right]) {
25+
left--;
26+
right++;
27+
}
28+
29+
return str.substring(left+1, right);
30+
}
31+
32+
//Expanding around center using redundant code block:- TC:O(n) SC:O(n)
33+
function longestPalindromicSubstring2(str) {
34+
let longestSubstr ="";
35+
let longestSubstrLen = 0;
36+
let left = 0, right = 0;
37+
38+
for(let i=0; i< str.length; i++) {
39+
//Odd length
40+
left = i; right = i;
41+
while(left >=0 && right < str.length && str[left] === str[right]) {
42+
if(right-left+1 > longestSubstrLen) {
43+
longestSubstr = str.substring(left, right+1);
44+
longestSubstrLen = right-left+1;
45+
}
46+
left--;
47+
right++;
48+
}
49+
//Even length
50+
left = i; right = i+1;
51+
while(left >=0 && right < str.length && str[left] === str[right]) {
52+
if(right-left+1 > longestSubstrLen) {
53+
longestSubstr = str.substring(left, right+1);
54+
longestSubstrLen = right-left+1;
55+
}
56+
left--;
57+
right;
58+
}
59+
}
60+
return longestSubstr;
61+
}
62+
63+
let str1 = "ababd";
64+
let str2 = "dbbc";
65+
console.log(longestPalindromicSubstring1(str1));
66+
console.log(longestPalindromicSubstring1(str2));
67+
console.log(longestPalindromicSubstring2(str1));
68+
console.log(longestPalindromicSubstring2(str2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
**Description:**
2+
Given a string `str`, return the longest palindromic substring of `str`.
3+
4+
**Note:**A palindrome is a string that reads the same backward as forward. If the characters includes non-alphanumerical letters, they needs to be removed and uppercase letters should be converted in a lowercase format. If there are multiple palindromic substrings that have the same length, return any one of them.
5+
6+
### Examples
7+
Example 1:
8+
Input: s = "ababd"
9+
Output: "aba"
10+
11+
Example 2:
12+
Input: s = "dbbc"
13+
Output: "bb"
14+
15+
**Algorithmic Steps**
16+
This problem is solved with the help of **expanding around center** approach where we expand the input string at each position. The algorithmic approach can be summarized as follows:
17+
18+
1. Add a preliminary check by returning `str` if the length of the string is less than or equal to 1.
19+
20+
2. Initialize longest palindromic substring(`maxPalindromicSubstr`) to first character of given string.
21+
22+
3. Iterate over an input string until the end of string.
23+
24+
4. Create a function(`expandAroundCenter`) to expand the string from center for each character position. This function iterates over the string based on left and right pointers. The left pointer will be decremented and right pointer will be incremented when the characters are equals from both the ends and also both pointers exists with in the range of string boundaries.
25+
26+
5. Invoke the function created in the previous step to find both odd(`left = right = i`) and even(`left = i`, `right = i+1`) longest palindromes.
27+
28+
6. Update the longest palindrome by comparing the current longest palindrome with odd and even palindromes.
29+
30+
7. Repeat steps 4-6 until all the characters traversed within given input string.
31+
32+
8. Return longest palindromic substring(`maxPalindromicSubstr`) from the main function.
33+
34+
**Time and Space complexity:**
35+
This algorithm has a time complexity of `O(n*2)` because expanding a palindrome around its center could take `O(n)` and it needs to repeated for each character(i.e, `n` times).
36+
37+
Also, it doesn't requires any additional data structure. So the space complexity is `O(1)`.

src/javascript/algorithms/strings/longestPalindromicSubstring.js

-34
This file was deleted.

0 commit comments

Comments
 (0)