How to use String Rotation and Concatenation In Javascript
This approach checks if the string can be found within a doubled version of itself (excluding the first and last characters). If the string is made up of the same substrings, it will appear within this modified doubled version.
Example:
function isMadeOfSameSubstrings(str) {
const doubledStr = str + str;
// Remove the first and last characters of the doubled string
const modifiedDoubledStr = doubledStr.substring(1, doubledStr.length - 1);
// Check if the original string appears in the modified doubled string
return modifiedDoubledStr.includes(str);
}
console.log(isMadeOfSameSubstrings("abcabcabc")); // Output: true
console.log(isMadeOfSameSubstrings("abcdefg")); // Output: false
Output
true false
How to check a string is entirely made up of the same substring in JavaScript ?
In this article, we are given a string and the task is to determine whether the string is made up of the same substrings or not.