-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path5. Longest Palindromic Substring.js
75 lines (72 loc) · 2.68 KB
/
5. Longest Palindromic Substring.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
69
70
71
72
73
74
75
/**
* @param {string} s
* @return {string}
*/
/**
Runtime: 928 ms, faster than 24.77% of JavaScript online submissions for Longest Palindromic Substring.
Memory Usage: 49.8 MB, less than 13.80% of JavaScript online submissions for Longest Palindromic Substring.
Date: 2/3/3033
Name: Bailey Kau
*/
//Check if a given string is a palindrome
//Also can be achieved using return str = str.split('').reverse().join('');
function isPalindrome(str) {
for (var i = 0; i < str.length/2; i++) {
if (str[i] != str[str.length - i - 1]) {
return false;
}
}
return true;
}
var longestPalindrome = function(str) {
//Edge Case of 1 letter or entire string is a palindrome (can led to infinite loops if not checked)
if (str.length == 1 || isPalindrome(str)) return str;
let expected = str[0];
//Need two palindrome strings and two substring booleans because we are checking even-length palindromes, then odd-length palindromes
let palindrome = "";
let palindrome2 = "";
let substring = true;
let substring2 = true;
for (var i = 1; i < str.length; i++) {
x = i - 1;
y = i + 2;
//This loop will continue until the substring calculated is not a palindrome
//Substring loop is for odd-length palindromes (aba, ababa)
//This is why x and y are 4 spaces apart (substring returns string not including last index)
while (substring && x > -1) {
substring = str.substring(x, y);
substring = isPalindrome(substring);
if (substring) {
palindrome = str.substring(x,y);
x--;
y++;
}
}
//Reset substring for the next iteration of the loop
substring = true;
z = i + 1;
x = i - 1;
//This loop will continue until the substring calculated is not a palindrome
//Substring loop is for even-length palindromes (abba, abccba)
//This is why x and y are 3 spaces apart (substring returns string not including last index)
while (substring2 && x > -1) {
substring2 = str.substring(x, z);
substring2 = isPalindrome(substring2);
if (substring2) {
palindrome2 = str.substring(x,z);
x--;
z++;
}
}
//Reset substring2 for next iteration of loop
substring2 = true;
//Change our output only if the 'palindrome' string is longer
if (palindrome.length > expected.length) {
expected = palindrome;
}
if (palindrome2.length > expected.length) {
expected = palindrome2;
}
}
return expected;
};