LeetCode.5 Longest Palindromic-substring (JS)

Keywords: Javascript

Topic 1

The longest palindrome substring:

Given a string s, find the longest palindrome substring in S. You can assume that the maximum length of S is 1000.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also an effective answer.

Example 2:

Input: "cbbd"
Output: "bb"

2. My Answer

  1. thinking

    1. Exclusion method, the optimal solution is certainly not violent traversal.
    2. Immediately after thinking about the method of traversing a string synchronously with two pointers used in the third question, I tried it and found that there was no difference between traversing a string with violence.
    3. Look at the question several times again. What we need to look for is the palindrome string. The character of the palindrome string is the same on both sides. That is to say, we need to look for the same middle point on both sides of the character. (Note that the middle point is not the middle character, because the middle point of example 1 in the title stem is a, but the middle point of example 2 is bb.)

  2. Code

    V1.0 (too ugly to skip the instructions and v2.0)

       /**
        * @param {string} s
        * @return {string}
        */
        var longestPalindrome = function(s) {
         function expendString(index1, index2) {
           if(index1 > 0 && index2 + 1 < s.length &&s[index1 -1] === s[index2 + 1]){
             return expendString(index1 -1, index2 + 1)
           } else {
             return [index1, index2, index2 - index1]
           }
         }
         let longestArr = [0, 0, 0]
         let tempArr
         for(let i = 1; i< s.length; i++) {
           if(i + 1 < s.length&&s[i-1] === s[i+1]) {
             tempArr = expendString(i-1, i+1)
             tempArr[2] > longestArr[2] ? longestArr = tempArr : null
           }
           if(s[i -1] === s[i]) {
             tempArr = expendString(i - 1, i)
             tempArr[2] > longestArr[2] ? longestArr = tempArr : null
           }
         }
         return s.slice(longestArr[0], longestArr[1] + 1)
       };

    explain

    1. The function expendString expands the string with two parameters to determine whether it is still a palindrome string.
    
    2. Array longestArr prevents the two subscripts and the length of the current longest string (I don't know why I bothered to write an array at that time).
    
    3. The for loop determines whether the middle point of the palindrome string is one ('aba') or two ('abba'), and then calls the expendString function defined above to expand it.
    

By the way, the next step is to optimize the version to be understandable.

           v2.0

/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function(s){
  function expendString(index1, index2) {
    if (index1 >= 0 && index2 < s.length && s[index1] === s[index2]) {
      expendString(index1 - 1, index2 + 1)
    } else {
      index2 - index1 > longestString.length ?
        longestString = s.slice(index1 + 1, index2) :
        null
    }
  }
  let longestString = s[0] || ''
  for (let i = 1; i < s.length; i++) {
    if (i + 1 < s.length) {
      expendString(i - 1, i + 1)
    }
    if (s[i - 1] === s[i]) {
      expendString(i - 1, i)
    }
  }
  return longestString
};

3. Excellent answers

to be continued

Fourth, the road is long and far-reaching

Posted by lancia on Sun, 07 Apr 2019 16:03:30 -0700