Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find longest palindrome javascript algorithm

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);
};
Comment

find longest palindrome javascript algorithm

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;
}
Comment

longest palindrome javascript algorithm

const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31));
// expected output: "the lazy dog."
Comment

PREVIOUS NEXT
Code Example
Javascript :: filter method javascript 
Javascript :: js array concat 
Javascript :: networkx get nodes 
Javascript :: javascript escape character 
Javascript :: selection sort javascript 
Javascript :: adding methods to objects javascript 
Javascript :: jquery slider 
Javascript :: javascript documentation 
Javascript :: modal slide from right 
Javascript :: string length javascript 
Javascript :: javascript zoom image onclick 
Javascript :: csurf in express 
Javascript :: javascript function add two numbers 
Javascript :: get all form errors angular 
Javascript :: return object from map javascript 
Javascript :: TypeError: this.setState is not a function 
Javascript :: google scripts get document 
Javascript :: The element.onclick Property 
Javascript :: round down html 
Javascript :: node express dynamic route and error handler 
Javascript :: google scripts urlfetchapp hearders and body 
Javascript :: how to check is the key of a localstorage is emopty 
Javascript :: javascript How to print every number that is divisible by either 3 or 5, but not both 
Javascript :: mariadb javascript 
Javascript :: rad client datasource refetch 
Javascript :: create new record mongoose 
Javascript :: while loop vs for loop javascript 
Javascript :: angular javascript 
Javascript :: datatables buttons do not appear localisation 
Javascript :: appinsights trackException javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =