Problem Solving/LeetCode
2023.11.10 Today's Challenge
fabichoi
2023. 11. 10. 23:45
https://leetcode.com/problems/count-number-of-homogenous-substrings/
Count Number of Homogenous Substrings - LeetCode
Can you solve this real interview question? Count Number of Homogenous Substrings - Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 7. A string is homogenous if all the characters
leetcode.com
지난번에 이어서 streak 이라는 개념을 통해 풀면 됨.
문제가 잘 이해가 안되네.. 아니면 너무 쓱 보고 지나가나 ㅎㅎ
class Solution:
def countHomogenous(self, s: str) -> int:
ans = 0
streak = 0
MOD = 10 ** 9 + 7
for i in range(len(s)):
if i == 0 or s[i] == s[i-1]:
streak += 1
else:
streak = 1
ans = (ans + streak) % MOD
return ans
반응형