πΉ Problem: 3330. Find the Original Typed String I
Difficulty: #Easy
Tags: #String
π Problem Summary
Given a string
word
, count how many possible strings you can form by changing some characters (but not necessarily all). In this context, the number of possible strings is increased each time two consecutive characters are equal. Return the total number of such possibilities.
π§ My Thought Process
Brute Force Idea:
Try to generate all possible string variations and count β this would be exponential (O(2^n)
), totally overkill for this problem.-
Optimized Strategy:
The pattern is much simpler: every time you encounter a pair of consecutive equal characters, it adds another possible way to modify the string.- Start with
ans = 1
(for the original string). - For each position
i
from1
ton-1
, ifword[i] == word[i-1]
, we can consider another possible string variation. - Just iterate once and count.
- Start with
βοΈ Code Implementation (Python)
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
β±οΈ Time & Space Complexity
- Time: O(n) β One pass through the string
- Space: O(1) β No extra space used
π§© Key Takeaways
- β You don't always need to simulate everything. Look for patterns.
- π‘ When a question asks about counting variations, always check if greedy one-pass observation is enough.
- π If I see a similar pattern with "consecutive matching characters", I'll think of this problem.
π Reflection (Self-Check)
- [x] Could I solve this without help?
- [x] Did I write code from scratch?
- [x] Did I understand why it works?
- [x] Will I be able to recall this in a week?
π Related Problems
- [[500 Keyboard Row]]
- [[2810 Faulty Keyboard]]
π Progress Tracker
Metric | Value |
---|---|
Day | 36 |
Total Problems Solved | 369 |
Confidence Today | π |
Top comments (0)