Skip to content
LC-3330 Easy LeetCode

3330. Find the Original Typed String I

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 57% Topics: String
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# array
class Solution(object):
    def possibleStringCount(self, word):
        """
        :type word: str
        :rtype: int
        """
        return len(word)-sum(word[i] != word[i+1] for i in xrange(len(word)-1))


# Time:  O(n)
# Space: O(1)
# array
class Solution2(object):
    def possibleStringCount(self, word):
        """
        :type word: str
        :rtype: int
        """
        result = 1
        curr = 0
        for i in xrange(len(word)):
            curr += 1
            if i+1 == len(word) or word[i+1] != word[i]:
                result += curr-1
                curr = 0
        return result

Solution from kamyu104/LeetCode-Solutions · MIT