
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is the string a combination of repeated substrings in JavaScript
Problem
We are required to write a JavaScript function that takes in a string of characters as the only argument. Our function needs to check if the string str can be constructed by taking a substring of it and appending multiple copies of the substring together.
For example, if the input to the function is −
const str = 'thisthisthisthis';
Then the output should be −
const output = true;
Output Explanation:
Because the string is made by appending ‘this’ string repeatedly.
Example
The code for this will be −
const str = 'thisthisthisthis'; const repeatedSubstring = (str = '') => { const {length} = str; const checkSubString = ss => { const m = ss.length; for (let i = 0; i < length; i += m) for (let j = 0; j < m; j++) if (str[i+j] !== ss[j]) return false; return true; }; let factor = 2, len; while (length/factor >= 1){ while (length % factor) factor++; len = length/factor; if (checkSubString(str.substring(0,len))){ return true; }; factor++; }; return false; }; console.log(repeatedSubstring(str));
Code Explanation:
Firstly, we set up substring pattern checking function.
Then we iterated through all possible factors that evenly divide string str, to determine if a viable repeat pattern has been found.
Output
And the output in the console will be −
true
- Related Articles
- Minimum steps to delete a string after repeated deletion of palindrome substrings in C++
- Get all substrings of a string in JavaScript recursively
- Segregating a string into substrings - JavaScript
- Finding the power of a string from a string with repeated letters in JavaScript
- Check if the string is a combination of strings in an array using JavaScript
- Unique substrings in circular string in JavaScript
- How to get the maximum count of repeated letters in a string? JavaScript
- Adding paragraph tag to substrings within a string in JavaScript
- Counting substrings of a string that contains only one distinct letter in JavaScript
- Separate a string with a special character sequence into a pair of substrings in JavaScript?
- Find K-Length Substrings With No Repeated Characters in Python
- Replacing Substrings in a Java String
- Can one string be repeated to form other in JavaScript
- Find distinct characters in distinct substrings of a string
- Find the Number of Substrings of a String using C++

Advertisements