-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathlongestPalindromicSubstring.js
68 lines (58 loc) · 2.08 KB
/
longestPalindromicSubstring.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Expanding around center:- TC:O(n) SC:O(n)
function longestPalindromicSubstring1(str) {
if(str.length <= 1) return str;
let maxPalindromicSubstr = str.substring(0, 1);
for (let i = 0; i < str.length; i++) {
let maxOddPalindromicSubstr = expandAroundCenter(str, i, i);
let maxEvenPalindromicSubstr = expandAroundCenter(str, i, i+1);
if(maxOddPalindromicSubstr.length > maxPalindromicSubstr.length) {
maxPalindromicSubstr = maxOddPalindromicSubstr;
}
if(maxEvenPalindromicSubstr.length > maxPalindromicSubstr.length) {
maxPalindromicSubstr = maxEvenPalindromicSubstr;
}
}
return maxPalindromicSubstr;
}
function expandAroundCenter(str, left, right) {
while(left >=0 && right < str.length && str[left] === str[right]) {
left--;
right++;
}
return str.substring(left+1, right);
}
//Expanding around center using redundant code block:- TC:O(n) SC:O(n)
function longestPalindromicSubstring2(str) {
let longestSubstr ="";
let longestSubstrLen = 0;
let left = 0, right = 0;
for(let i=0; i< str.length; i++) {
//Odd length
left = i; right = i;
while(left >=0 && right < str.length && str[left] === str[right]) {
if(right-left+1 > longestSubstrLen) {
longestSubstr = str.substring(left, right+1);
longestSubstrLen = right-left+1;
}
left--;
right++;
}
//Even length
left = i; right = i+1;
while(left >=0 && right < str.length && str[left] === str[right]) {
if(right-left+1 > longestSubstrLen) {
longestSubstr = str.substring(left, right+1);
longestSubstrLen = right-left+1;
}
left--;
right++;
}
}
return longestSubstr;
}
let str1 = "ababd";
let str2 = "dbbc";
console.log(longestPalindromicSubstring1(str1));
console.log(longestPalindromicSubstring1(str2));
console.log(longestPalindromicSubstring2(str1));
console.log(longestPalindromicSubstring2(str2));