How to check a string is entirely made up of the same substring in JavaScript ?
Last Updated :
24 Jun, 2024
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.
JavaScript
// 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);
OutputString 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.
JavaScript
// 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);
OutputString 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:
JavaScript
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
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:
JavaScript
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
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:
JavaScript
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
Similar Reads
JavaScript - How To Check Whether a String Contains a Substring? Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false oth
3 min read
Javascript Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output
2 min read
How to check the given string is palindrome using JavaScript ? A palindrome is a word, sentence, or even number that reads the same from the back and from the front. Therefore if we take the input, reverse the string and check if the reversed string and the original string are equal, it means the string is a palindrome, otherwise, it is not. Approach: When the
3 min read
How to check a given string is an anagram of another string in JavaScript ? In this article, we will learn how to check a given string is an anagram of another string in JavaScript. Before that, we should learn what is an anagram.An anagram is a word or sentence, which usually contains all the original letters exactly once, in order to arrange the letters of a different ter
3 min read
How To Split a String Into Segments Of n Characters in JavaScript? When working with JavaScript, you might encounter a situation where you need to break a string into smaller segments of equal lengths. This could be useful when formatting a serial number, creating chunks of data for processing, or splitting a long string into manageable pieces. In this article, we
6 min read