[Leetcode] 3330. Find the Original Typed String I

whitehousechef·2025년 7월 1일

https://leetcode.com/problems/find-the-original-typed-string-i/description/?envType=daily-question&envId=2025-07-01

initial

for this question, I didnt know we had to think about the position of each character so I just used Counter. But if we have like "ere", if we use counter it gives us wrong ans of 2 but actually answer is 1. So we have to just find the consecutive duplicate characters, which by doing so we are considering the order (consecutive characters)

from collections import Counter
class Solution:
    def possibleStringCount(self, word: str) -> int:
        counter = Counter(word)
        ans=1
        for key,val in counter.items():
            if val>1:
                ans+=val-1
        return ans

revisited 8/8 2025

key is u press one key at most once. So i got it this time ez

sol

class Solution:
    def possibleStringCount(self, word: str) -> int:
        ans=1
        tmp=""
        tmp_count=1
        for c in word:
            if tmp!=c:
                tmp=c
            else:
                tmp_count+=1
                if tmp_count>1:
                    ans+=1
        return ans
            

complexity

n time 1 space

0개의 댓글