less than 1 minute read

Problem Statement

leetcode problem link

Brute Force [Accepted]

class Solution:
    def possibleStringCount(self, word: str) -> int:
        length = len(word)
        res = 0
        curr = word[0]
        count = 0
        for i, ch in enumerate(word):
            if ch != curr:
                res += count - 1
                count = 0
            count += 1
            curr = ch
        res += count - 1
        return res + 1

Editorial

Approach: One-time Traversal

class Solution:
    def possibleStringCount(self, word: str) -> int:
        n, ans = len(word), 1
        for i in range(1, n):
            if word[i - 1] == word[i]:
                ans += 1
        return ans