algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

count-rotations-with-exactly-k-equal-adjacent-pairs.py (654B)


      1 class Solution:
      2 
      3     def prefix_append(self,num,s):
      4         pre = s[0:num]
      5         s = s[num:len(s)] + pre
      6         return s
      7     def score(self,s):
      8         sc = 0
      9         for i in range(0,len(s) - 1):
     10             if s[i] == s[i+1]:
     11                 sc += 1
     12         return sc
     13         
     14     def countRotations(self, s: str, k: int) -> int:
     15         # string s length n, int k
     16         # choose prefix of s and move it to the end of string
     17         # score = number of i where i == i+1
     18         score = 0
     19         for n in range(0,len(s)):
     20             re = self.prefix_append(n,s)
     21             if self.score(re) == k:
     22                 score += 1
     23         return score