class Solution:
def countLetters(self, S: str) -> int:
total = left = 0
for right in range(len(S) + 1):
if right == len(S) or S[left] != S[right]:
len_substring = right - left
# more details about the sum of the arithmetic sequence:
# https://en.wikipedia.org/wiki/Arithmetic_progression#Sum
total += (1 + len_substring) * len_substring // 2
left = right
return totalTime complexity:
Space complexity: constant space
Where is the length of the input string
s.
class Solution:
def countLetters(self, S: str) -> int:
total = 1
substrings = [0] * len(S)
substrings[0] = 1
for i in range(1, len(S)):
if S[i - 1] == S[i]:
substrings[i] = substrings[i-1] + 1
else:
substrings[i] = 1
total += substrings[i]
return totalTime complexity:
Space complexity:
Where is the length of the input string
s.
class Solution:
def countLetters(self, S: str) -> int:
total = 1
count = 1
for i in range(1, len(S)):
if S[i] == S[i-1]:
count += 1
else:
count = 1
total += count
return totalTime complexity:
Space complexity: constant space
Where is the length of the input string
s.