Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. // O(s.length + ans.length)
  2. function minimum_cycle(s){
  3. const L = s.length
  4. if(L <= 1) return s
  5.  
  6. var i=0, j=1, cnt=0
  7. while(cnt < L){
  8. if(s[i] === s[j]){
  9. i = (i+1)%L
  10. j = (j+1)%L
  11. cnt++
  12. } else {
  13. j = (j+1)%L
  14. cnt = 0
  15. }
  16. }
  17.  
  18. if(i < j) return s.slice(i,j)
  19. return s.slice(i) + s.slice(0,j)
  20. }
  21.  
  22. console.log(cycle("101001101001101001101001")) // 110100
  23. console.log(cycle("10111001001011100100")) // 0010010111
  24. console.log(cycle("111000110010111000110010111000110010")) // 000110010111
  25. console.log(cycle("1011101110111011101110111011")) // 0111
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement