Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  185] [ 7]  / answers: 1 / hits: 19597  / 5 Years ago, wed, april 24, 2019, 12:00:00

I have to create a function which takes a string, and it should return true or false based on whether the input consists of a repeated character sequence. The length of the given string is always greater than 1 and the character sequence must have at least one repetition.



aa // true(entirely contains two strings a)
aaa //true(entirely contains three string a)
abcabcabc //true(entirely containas three strings abc)

aba //false(At least there should be two same substrings and nothing more)
ababa //false(ab exists twice but a is extra so false)


I have created the below function:





function check(str){
if(!(str.length && str.length - 1)) return false;
let temp = '';
for(let i = 0;i<=str.length/2;i++){
temp += str[i]
//console.log(str.replace(new RegExp(temp,g),''))
if(!str.replace(new RegExp(temp,g),'')) return true;
}
return false;
}

console.log(check('aa')) //true
console.log(check('aaa')) //true
console.log(check('abcabcabc')) //true
console.log(check('aba')) //false
console.log(check('ababa')) //false





Checking of this is part of the real problem. I can't afford a non-efficient solution like this. First of all, it's looping through half of the string.



The second problem is that it is using replace() in each loop which makes it slow. Is there a better solution regarding performance?


More From » string

 Answers
18

There’s a nifty little theorem about strings like these.



A string consists of the same pattern repeated multiple times if and only if the string is a nontrivial rotation of itself.



Here, a rotation means deleting some number of characters from the front of the string and moving them to the back. For example, the string hello could be rotated to form any of these strings:


hello (the trivial rotation)
elloh
llohe
lohel
ohell

To see why this works, first, assume that a string consists of k repeated copies of a string w. Then deleting the first copy of the repeated pattern (w) from the front of the string and tacking it onto the back will give back the same string. The reverse direction is a bit trickier to prove, but the idea is that if you rotate a string and get back what you started with, you can apply that rotation repeatedly to tile the string with multiple copies of the same pattern (that pattern being the string you needed to move to the end to do the rotation).


Now the question is how to check whether this is the case. For that, there’s another beautiful theorem we can use:



If x and y are strings of the same length, then x is a rotation of y if and only if x is a substring of yy.



As an example, we can see that lohel is a rotation of hello as follows:


hellohello
^^^^^

In our case, we know that every string x will always be a substring of xx (it’ll appear twice, once at each copy of x). So basically we just need to check if our string x is a substring of xx without allowing it to match at the first or halfway character. Here’s a one-liner for that:


function check(str) {
return (str + str).indexOf(str, 1) !== str.length;
}

Assuming indexOf is implemented using a fast string matching algorithm, this will run in time O(n), where n is the length of the input string.


[#52196] Thursday, April 18, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maya

Total Points: 418
Total Questions: 116
Total Answers: 112

Location: Mauritania
Member since Sun, Oct 17, 2021
3 Years ago
;