Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // O(s.length + ans.length)
- function minimum_cycle(s){
- const L = s.length
- if(L <= 1) return s
- var i=0, j=1, cnt=0
- while(cnt < L){
- if(s[i] === s[j]){
- i = (i+1)%L
- j = (j+1)%L
- cnt++
- } else {
- j = (j+1)%L
- cnt = 0
- }
- }
- if(i < j) return s.slice(i,j)
- return s.slice(i) + s.slice(0,j)
- }
- console.log(cycle("101001101001101001101001")) // 110100
- console.log(cycle("10111001001011100100")) // 0010010111
- console.log(cycle("111000110010111000110010111000110010")) // 000110010111
- console.log(cycle("1011101110111011101110111011")) // 0111
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement