How to check a string is entirely made up of the same substring in JavaScript ?
We are given a string and the task is to determine whether the string is made up of the same substrings or not.
1. Using Regular Expression with test() method
- Initialize a string to the variable.
- Use the test() method to test a pattern in a string.
- The test() method returns true if the match is found, otherwise, it returns false.
Example 1: This example checks for string geeksgeeksgeeks which is made up of the same substring, so it returns True.
// Input string
let str = "geeksgeeksgeeks";
// Function to check if string is made of same substring
function check(str) {
return /^(.+)\1+$/.test(str)
}
let ans = "String is not made up of same substrings";
if (check(str)) {
ans = "String is made up of same substrings";
}
// Display output
console.log(ans);
Output
String is made up of same substrings
Example 2: This example checks for string geeksgeekgeeks which is not made up of the same substrings so it returns False.
// Input string
let str = "geeksgeekgeeks";
// Function to check if string is made of same substring
function check(str) {
return /^(.+)\1+$/.test(str)
}
let ans = "String is not made up of same substrings";
if (check(str)) {
ans = "String is made up of same substrings";
}
// Display output
console.log(ans);
Output
String is not made up of same substrings
2. Using String.prototype.repeat() and String.prototype.startsWith()
This approach repeats a substring until it equals the input string's length. Then, it checks if the input string starts with this repeated substring, indicating that the string is entirely made up of the same substring.
Example:
function isEntirelyMadeUpOfSameSubstring(str) {
const len = str.length;
for (let i = 1; i <= len / 2; i++) {
const substring = str.substring(0, i);
if (substring.repeat(len / i) === str) {
return true;
}
}
return false;
}
console.log(isEntirelyMadeUpOfSameSubstring("abcabcabc")); // Output: true
console.log(isEntirelyMadeUpOfSameSubstring("abcdefg")); // Output: false
Output
true false
3. Using String Rotation and Concatenation
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
Using Divide and Conquer with Array.prototype.every()
This approach divides the string into possible substrings and checks if all parts of the string are equal to the candidate substring using the every() method. If all parts are equal, the string is made up of the same substring.
Example:
function isMadeOfSameSubstring(str) {
const length = str.length;
for (let i = 1; i <= length / 2; i++) {
if (length % i === 0) {
const candidate = str.substring(0, i);
const repeated = Array(length / i).fill(candidate).join('');
if (repeated === str) {
return true;
}
}
}
return false;
}
// Example usage:
const str1 = "abcabcabc";
const str2 = "abcdabc";
console.log(isMadeOfSameSubstring(str1)); // Output: true
console.log(isMadeOfSameSubstring(str2)); // Output: false
Output
true false