Skip to content

Commit c95b9cc

Browse files
committed
Sync LeetCode submission Runtime - 44 ms (24.23%), Memory - 17.6 MB (72.11%)
1 parent 6a13435 commit c95b9cc

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<p>Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and <strong>may</strong> press a key for too long, resulting in a character being typed <strong>multiple</strong> times.</p>
2+
3+
<p>Although Alice tried to focus on her typing, she is aware that she may still have done this <strong>at most</strong> <em>once</em>.</p>
4+
5+
<p>You are given a string <code>word</code>, which represents the <strong>final</strong> output displayed on Alice&#39;s screen.</p>
6+
7+
<p>Return the total number of <em>possible</em> original strings that Alice <em>might</em> have intended to type.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<div class="example-block">
13+
<p><strong>Input:</strong> <span class="example-io">word = &quot;abbcccc&quot;</span></p>
14+
15+
<p><strong>Output:</strong> <span class="example-io">5</span></p>
16+
17+
<p><strong>Explanation:</strong></p>
18+
19+
<p>The possible strings are: <code>&quot;abbcccc&quot;</code>, <code>&quot;abbccc&quot;</code>, <code>&quot;abbcc&quot;</code>, <code>&quot;abbc&quot;</code>, and <code>&quot;abcccc&quot;</code>.</p>
20+
</div>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<div class="example-block">
25+
<p><strong>Input:</strong> <span class="example-io">word = &quot;abcd&quot;</span></p>
26+
27+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
28+
29+
<p><strong>Explanation:</strong></p>
30+
31+
<p>The only possible string is <code>&quot;abcd&quot;</code>.</p>
32+
</div>
33+
34+
<p><strong class="example">Example 3:</strong></p>
35+
36+
<div class="example-block">
37+
<p><strong>Input:</strong> <span class="example-io">word = &quot;aaaa&quot;</span></p>
38+
39+
<p><strong>Output:</strong> <span class="example-io">4</span></p>
40+
</div>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= word.length &lt;= 100</code></li>
47+
<li><code>word</code> consists only of lowercase English letters.</li>
48+
</ul>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Approach 1: One-time traversal
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def possibleStringCount(self, word: str) -> int:
8+
n, ans = len(word), 1
9+
10+
for i in range(1, n):
11+
if word[i - 1] == word[i]:
12+
ans += 1
13+
return ans

0 commit comments

Comments
 (0)