-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubStringPalindrome.js
More file actions
32 lines (28 loc) · 1014 Bytes
/
subStringPalindrome.js
File metadata and controls
32 lines (28 loc) · 1014 Bytes
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
// Given a string, return the length of the longest Palindorme in the string.
function subStringPalindrome(str) {
const arr = [];
const splittedStr = str.toLowerCase().replace(/[.,!]/g, "").split(" ");
for (let i = 0; i < splittedStr.length; i++) {
const wordSplit = splittedStr[i].split("").reverse().join("");
if (wordSplit === splittedStr[i]) {
arr.push(wordSplit.length);
}
}
return Math.max(...arr);
}
console.log(subStringPalindrome("madam is going to kayak"));
// OR
function subStringPalindromeTwo(str) {
const arr = [];
const splittedStr = str.toLowerCase().replace(/[.,!]/g, "").split(" ");
for (let i = 0; i < splittedStr.length; i++) {
let word = "";
for (let j = splittedStr[i].length - 1; j >= 0; j--) {
word += splittedStr[i][j];
}
if (word === splittedStr[i]) arr.push(word.length);
}
return Math.max(...arr);
}
console.log(subStringPalindromeTwo("madam"));
console.log(subStringPalindromeTwo("madam is going to kayak to repaper."));