How to check if second string a rotated version of first string in Node.js

1 Answer

0 votes
const secondStringRotatedOfFirstString = (first, second) => {
    if (first.length !== second.length){
        return false
    };

    for (let i = 0; i < first.length; i++) {
        const rotated = first.slice(i).concat(first.slice(0, i));
        if (rotated === second) {
            return true
        };
    }
    
    return false;
};

const first = 'abcdefgh';  
const second = 'cdefghab';

console.log(secondStringRotatedOfFirstString(first, second));




/*
run:

true

*/

 



answered Mar 21, 2024 by avibootz

Related questions

...