Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1 keep track of the cnt of one uniq char suffix length
- #O(n) time and 0(1) xtra space
- class Solution(object):
- def maxPower(self, s):
- res = cnt = 1
- for i in range(1, len(s)):
- if s[i] == s[i-1]:
- cnt += 1
- else:
- #take max only when previous suffix is reset
- res, cnt = max(res, cnt), 1
- #for cases where the suffix of entire string is the ans
- #if we take max in if part then this is not required
- return max(res, cnt)
- #2 keep track of start of one uniq char suffix. This is two pointer/sliding window approach where window = largest suffix made of one character, start = st and end = i (the two pointers) in every iteration
- '''
- class Solution(object):
- def maxPower(self, s):
- res, st = 1, 0
- for i in range(1, len(s)):
- #reset the start of suffix
- if s[i] != s[i-1]:
- res, st = max(res, i-st), i
- return max(res, len(s)-st)
- '''
- #mod of #1 by keeping track of the char of suffix (previous character) which can be used in place of s[i-1]
- '''
- class Solution(object):
- def maxPower(self, s):
- res,char,cnt = 0,' ',0
- for e in s:
- if e == char:
- cnt += 1
- else:
- res, char, cnt = max(res, cnt), e, 1
- return max(res, cnt)
- '''
Add Comment
Please, Sign In to add comment