Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if a string is entirely made of the same substring JavaScript
In JavaScript, you can check if a string is entirely made of repeated substrings using various approaches. This is useful for pattern validation and string analysis.
The problem requires that the string consists of a repeated character sequence with at least one repetition. For example, "aa" contains two "a" substrings, "abcabcabc" contains three "abc" substrings, but "ababa" fails because it has an extra character.
Examples of Valid and Invalid Patterns
- "aa" should return true because it entirely contains two strings "a"
- "aaa" should return true because it entirely contains three strings "a"
- "abcabcabc" should return true because it entirely contains three strings "abc"
- "aba" should return false because there should be at least two same substrings and nothing more
- "ababa" should return false because "ab" exists twice but "a" is extra
Method 1: Using Substring Iteration
This approach checks all possible substring lengths and verifies if they repeat throughout the entire string:
const checkCombination = (str = '') => {
if(str.length == 1) {
return true;
}
for(let i = 1; i {
if(str.length > sub.length){
let left = str.substring(0, sub.length);
let right = str.substring(sub.length, str.length);
return left === sub && isRepeating(sub, right);
}
return str === sub;
}
console.log(checkCombination('aa'));
console.log(checkCombination('aaa'));
console.log(checkCombination('abcabcabc'));
console.log(checkCombination('aba'));
console.log(checkCombination('ababa'));
true true true false false
Method 2: Using String Repeat Method
A more concise approach using the built-in repeat() method:
const checkRepeatedSubstring = (str) => {
const len = str.length;
for(let i = 1; i
true
true
true
false
Method 3: Using Regular Expressions
An elegant solution using regex pattern matching:
const checkWithRegex = (str) => {
return /^(.+)\1+$/.test(str);
}
console.log(checkWithRegex('aa'));
console.log(checkWithRegex('abcabcabc'));
console.log(checkWithRegex('123123'));
console.log(checkWithRegex('abcde'));
true
true
true
false
Comparison of Methods
| Method | Time Complexity | Readability | Best For |
|---|---|---|---|
| Substring Iteration | O(n²) | Good | Understanding the logic |
| String Repeat | O(n²) | Excellent | Clean, readable code |
| Regular Expression | O(n) | Fair | Concise one-liners |
How It Works
All methods follow the same principle: they check divisors of the string length and verify if repeating a substring of that length recreates the original string. The key insight is that if a string is made of repeated substrings, the substring length must be a divisor of the total string length.
Conclusion
The string repeat method offers the best balance of readability and performance for checking repeated substrings. Use regular expressions for concise solutions, and substring iteration when you need to understand the underlying logic.
