Skip to main content

Check if a string consists of a repeated character sequence

JavaScript version

const consistsRepeatedSubstring = (str) => `${str}${str}`.indexOf(str, 1) !== str.length;

TypeScript version

const consistsRepeatedSubstring = (str: string): boolean => `${str}${str}`.indexOf(str, 1) !== str.length;

Examples

consistsRepeatedSubstring('aa'); // true
consistsRepeatedSubstring('aaa'); // true
consistsRepeatedSubstring('ababab'); // true
consistsRepeatedSubstring('abc'); // false

Comments