“Trouver le plus long algorithme Javascript Palindrome” Réponses codées

Trouver le plus long algorithme Javascript Palindrome

var longestPalindrome = function(s) {
    let start, end, result = 0;
    
    for(let i=0; i < s.length; i++) {
        let tempStart = i;
        let tempEnd = i;
        
        while(s[tempEnd] === s[tempEnd+1]) tempEnd++;
        
        while(tempStart >= 0 && tempEnd < s.length && s[tempStart-1] == s[tempEnd+1]) {
            tempStart--;
            tempEnd++
        }
        
        if(result < tempEnd - tempStart+1) {
            result = tempEnd - tempStart+1;
            start = tempStart;
            end = tempEnd;
        }
    }
    return s.substring(start, end+1);
};
Restu Wahyu Saputra

Trouver le plus long algorithme Javascript Palindrome

const longestPalindrome = (s) => {
    if(s === s.split('').reverse().join('')) return s;
    let charArray = [], 
	countArray = [];
      
    let currentWord = s[0];
    let count = 1;
    let sLength = s.length+1;
    
    //build array of repeating characters
    for(let i=1; i<sLength; i++) {
        if(s[i] === s[i-1]) {
            //simply advance by incrementing count and character to currentWord
            count++;
            currentWord += s[i];
        } else { //different character found
            //save character & count
            countArray.push(count);
            charArray.push(currentWord);
            
            //reset currentWord, count
            currentWord = s[i];
            count = 1;
        }
    }

    let head = 0;
    let tail = 0;
    let maxWord = '';
    let charLength = charArray.length;
    //step through character array to check for characters surrounding repeated characters
    for(let k=0; k<charLength; k++){
        currentWord = charArray[k];
        tail = head + countArray[k] - 1;
        let steps = sLength-countArray[k]; //num of steps we need to take surrounding repeated characters
        for(let l=1; l<steps; l++){
            if(s[head-l] === s[tail+l]) {
                currentWord = s[head-l] + currentWord + s[tail+l];
            } else {
                //check currentWord against maxWord
                maxWord = maxWord.length < currentWord.length ? currentWord : maxWord;
                head = tail+1;
                break;
            }
        }
    }
    return maxWord;
}
Restu Wahyu Saputra

Réponses similaires à “Trouver le plus long algorithme Javascript Palindrome”

Questions similaires à “Trouver le plus long algorithme Javascript Palindrome”

Plus de réponses similaires à “Trouver le plus long algorithme Javascript Palindrome” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code