파비의 매일매일 공부기록

2023.11.10 Today's Challenge 본문

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
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.11.12 Today's Challenge  (0) 2023.11.12
2023.11.11 Today's Challenge  (1) 2023.11.11
2023.11.09 Today's Challenge  (0) 2023.11.09
2023.11.08 Today's Challenge  (0) 2023.11.08
2023.11.07 Today's Challenge  (0) 2023.11.07
Comments