How to check if strings are rotations of each other in Node.js

1 Answer

0 votes
function rotations(str1, str2) {
    if (str1.length != str2.length)
        return false;
         
    console.log(str1 + str1);
     
    return (str1 + str1).indexOf(str2) != -1;
}
      
 
const str1 = "abcxd";
const str2 = "cxdab";
  
if (rotations(str1, str2))
    console.log("yes");
else
    console.log("no");
     
     
     
     
/*
run:
 
abcxdabcxd
yes
 
*/

 



answered Dec 23, 2021 by avibootz

Related questions

...