|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3330.Find%20the%20Original%20Typed%20String%20I/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3330. Find the Original Typed String I](https://leetcode.com/problems/find-the-original-typed-string-i) |
| 10 | + |
| 11 | +[中文文档](/solution/3300-3399/3330.Find%20the%20Original%20Typed%20String%20I/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<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> |
| 18 | + |
| 19 | +<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> |
| 20 | + |
| 21 | +<p>You are given a string <code>word</code>, which represents the <strong>final</strong> output displayed on Alice's screen.</p> |
| 22 | + |
| 23 | +<p>Return the total number of <em>possible</em> original strings that Alice <em>might</em> have intended to type.</p> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong class="example">Example 1:</strong></p> |
| 27 | + |
| 28 | +<div class="example-block"> |
| 29 | +<p><strong>Input:</strong> <span class="example-io">word = "abbcccc"</span></p> |
| 30 | + |
| 31 | +<p><strong>Output:</strong> <span class="example-io">5</span></p> |
| 32 | + |
| 33 | +<p><strong>Explanation:</strong></p> |
| 34 | + |
| 35 | +<p>The possible strings are: <code>"abbcccc"</code>, <code>"abbccc"</code>, <code>"abbcc"</code>, <code>"abbc"</code>, and <code>"abcccc"</code>.</p> |
| 36 | +</div> |
| 37 | + |
| 38 | +<p><strong class="example">Example 2:</strong></p> |
| 39 | + |
| 40 | +<div class="example-block"> |
| 41 | +<p><strong>Input:</strong> <span class="example-io">word = "abcd"</span></p> |
| 42 | + |
| 43 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 44 | + |
| 45 | +<p><strong>Explanation:</strong></p> |
| 46 | + |
| 47 | +<p>The only possible string is <code>"abcd"</code>.</p> |
| 48 | +</div> |
| 49 | + |
| 50 | +<p><strong class="example">Example 3:</strong></p> |
| 51 | + |
| 52 | +<div class="example-block"> |
| 53 | +<p><strong>Input:</strong> <span class="example-io">word = "aaaa"</span></p> |
| 54 | + |
| 55 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 56 | +</div> |
| 57 | + |
| 58 | +<p> </p> |
| 59 | +<p><strong>Constraints:</strong></p> |
| 60 | + |
| 61 | +<ul> |
| 62 | + <li><code>1 <= word.length <= 100</code></li> |
| 63 | + <li><code>word</code> consists only of lowercase English letters.</li> |
| 64 | +</ul> |
| 65 | + |
| 66 | +<!-- description:end --> |
| 67 | + |
| 68 | +## Solutions |
| 69 | + |
| 70 | +<!-- solution:start --> |
| 71 | + |
| 72 | +### Solution 1 |
| 73 | + |
| 74 | +<!-- tabs:start --> |
| 75 | + |
| 76 | +#### Python3 |
| 77 | + |
| 78 | +```python |
| 79 | +class Solution: |
| 80 | + def possibleStringCount(self, word: str) -> int: |
| 81 | + return 1 + sum(x == y for x, y in pairwise(word)) |
| 82 | +``` |
| 83 | + |
| 84 | +#### Java |
| 85 | + |
| 86 | +```java |
| 87 | +class Solution { |
| 88 | + public int possibleStringCount(String word) { |
| 89 | + int f = 1; |
| 90 | + for (int i = 1; i < word.length(); ++i) { |
| 91 | + if (word.charAt(i) == word.charAt(i - 1)) { |
| 92 | + ++f; |
| 93 | + } |
| 94 | + } |
| 95 | + return f; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +#### C++ |
| 101 | + |
| 102 | +```cpp |
| 103 | +class Solution { |
| 104 | +public: |
| 105 | + int possibleStringCount(string word) { |
| 106 | + int f = 1; |
| 107 | + for (int i = 1; i < word.size(); ++i) { |
| 108 | + f += word[i] == word[i - 1]; |
| 109 | + } |
| 110 | + return f; |
| 111 | + } |
| 112 | +}; |
| 113 | +``` |
| 114 | +
|
| 115 | +#### Go |
| 116 | +
|
| 117 | +```go |
| 118 | +func possibleStringCount(word string) int { |
| 119 | + f := 1 |
| 120 | + for i := 1; i < len(word); i++ { |
| 121 | + if word[i] == word[i-1] { |
| 122 | + f++ |
| 123 | + } |
| 124 | + } |
| 125 | + return f |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +#### TypeScript |
| 130 | + |
| 131 | +```ts |
| 132 | +function possibleStringCount(word: string): number { |
| 133 | + let f = 1; |
| 134 | + for (let i = 1; i < word.length; ++i) { |
| 135 | + f += word[i] === word[i - 1] ? 1 : 0; |
| 136 | + } |
| 137 | + return f; |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +<!-- tabs:end --> |
| 142 | + |
| 143 | +<!-- solution:end --> |
| 144 | + |
| 145 | +<!-- problem:end --> |
0 commit comments