Longest Chunked Palindrome Decomposition LeetCode solution in JavaScript

1
var longestDecomposition = function (text) {
2
var i = 1;
3
var output = 0;
4
while (i < text.length) {
5
if (text.substring(0, i) == text.substring(text.length - i)) {
6
output += 2; //add 2 to simulate adding to both sides of output array
7
text = text.substring(i, text.length - i); //cut text to simulate popping off of both sides
8
i = 1;
9
} else {
10
i++;
11
}
12
}
13

14
return text ? output + 1 : output; //if there's any text leftover that didn't have a match, it's the middle and would add 1 to output array
15
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0