Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7fc3166

Browse files
feat: add new lc problems (doocs#3676)
1 parent b1b3576 commit 7fc3166

File tree

33 files changed

+2500
-1
lines changed

33 files changed

+2500
-1
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3330.Find%20the%20Original%20Typed%20String%20I/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3330. 找到初始输入字符串 I](https://leetcode.cn/problems/find-the-original-typed-string-i)
10+
11+
[English Version](/solution/3300-3399/3330.Find%20the%20Original%20Typed%20String%20I/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>Alice 正在她的电脑上输入一个字符串。但是她打字技术比较笨拙,她&nbsp;<strong>可能</strong>&nbsp;在一个按键上按太久,导致一个字符被输入&nbsp;<strong>多次</strong>&nbsp;。</p>
18+
19+
<p>尽管 Alice 尽可能集中注意力,她仍然可能会犯错 <strong>至多</strong>&nbsp;一次。</p>
20+
21+
<p>给你一个字符串&nbsp;<code>word</code> ,它表示 <strong>最终</strong>&nbsp;显示在 Alice 显示屏上的结果。</p>
22+
23+
<p>请你返回 Alice 一开始可能想要输入字符串的总方案数。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong class="example">示例 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><span class="example-io"><b>输入:</b>word = "abbcccc"</span></p>
31+
32+
<p><span class="example-io"><b>输出:</b>5</span></p>
33+
34+
<p><strong>解释:</strong></p>
35+
36+
<p>可能的字符串包括:<code>"abbcccc"</code>&nbsp;,<code>"abbccc"</code>&nbsp;,<code>"abbcc"</code>&nbsp;,<code>"abbc"</code>&nbsp;&nbsp;<code>"abcccc"</code>&nbsp;。</p>
37+
</div>
38+
39+
<p><strong class="example">示例 2:</strong></p>
40+
41+
<div class="example-block">
42+
<p><span class="example-io"><b>输入:</b>word = "abcd"</span></p>
43+
44+
<p><span class="example-io"><b>输出:</b>1</span></p>
45+
46+
<p><strong>解释:</strong></p>
47+
48+
<p>唯一可能的字符串是&nbsp;<code>"abcd"</code>&nbsp;。</p>
49+
</div>
50+
51+
<p><strong class="example">示例 3:</strong></p>
52+
53+
<div class="example-block">
54+
<p><span class="example-io"><b>输入:</b>word = "aaaa"</span></p>
55+
56+
<p><span class="example-io"><b>输出:</b>4</span></p>
57+
</div>
58+
59+
<p>&nbsp;</p>
60+
61+
<p><strong>提示:</strong></p>
62+
63+
<ul>
64+
<li><code>1 &lt;= word.length &lt;= 100</code></li>
65+
<li><code>word</code>&nbsp;只包含小写英文字母。</li>
66+
</ul>
67+
68+
<!-- description:end -->
69+
70+
## 解法
71+
72+
<!-- solution:start -->
73+
74+
### 方法一
75+
76+
<!-- tabs:start -->
77+
78+
#### Python3
79+
80+
```python
81+
class Solution:
82+
def possibleStringCount(self, word: str) -> int:
83+
return 1 + sum(x == y for x, y in pairwise(word))
84+
```
85+
86+
#### Java
87+
88+
```java
89+
class Solution {
90+
public int possibleStringCount(String word) {
91+
int f = 1;
92+
for (int i = 1; i < word.length(); ++i) {
93+
if (word.charAt(i) == word.charAt(i - 1)) {
94+
++f;
95+
}
96+
}
97+
return f;
98+
}
99+
}
100+
```
101+
102+
#### C++
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int possibleStringCount(string word) {
108+
int f = 1;
109+
for (int i = 1; i < word.size(); ++i) {
110+
f += word[i] == word[i - 1];
111+
}
112+
return f;
113+
}
114+
};
115+
```
116+
117+
#### Go
118+
119+
```go
120+
func possibleStringCount(word string) int {
121+
f := 1
122+
for i := 1; i < len(word); i++ {
123+
if word[i] == word[i-1] {
124+
f++
125+
}
126+
}
127+
return f
128+
}
129+
```
130+
131+
#### TypeScript
132+
133+
```ts
134+
function possibleStringCount(word: string): number {
135+
let f = 1;
136+
for (let i = 1; i < word.length; ++i) {
137+
f += word[i] === word[i - 1] ? 1 : 0;
138+
}
139+
return f;
140+
}
141+
```
142+
143+
<!-- tabs:end -->
144+
145+
<!-- solution:end -->
146+
147+
<!-- problem:end -->
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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&#39;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>&nbsp;</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 = &quot;abbcccc&quot;</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>&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>
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 = &quot;abcd&quot;</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>&quot;abcd&quot;</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 = &quot;aaaa&quot;</span></p>
54+
55+
<p><strong>Output:</strong> <span class="example-io">4</span></p>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
<p><strong>Constraints:</strong></p>
60+
61+
<ul>
62+
<li><code>1 &lt;= word.length &lt;= 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 -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int possibleStringCount(string word) {
4+
int f = 1;
5+
for (int i = 1; i < word.size(); ++i) {
6+
f += word[i] == word[i - 1];
7+
}
8+
return f;
9+
}
10+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func possibleStringCount(word string) int {
2+
f := 1
3+
for i := 1; i < len(word); i++ {
4+
if word[i] == word[i-1] {
5+
f++
6+
}
7+
}
8+
return f
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int possibleStringCount(String word) {
3+
int f = 1;
4+
for (int i = 1; i < word.length(); ++i) {
5+
if (word.charAt(i) == word.charAt(i - 1)) {
6+
++f;
7+
}
8+
}
9+
return f;
10+
}
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def possibleStringCount(self, word: str) -> int:
3+
return 1 + sum(x == y for x, y in pairwise(word))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function possibleStringCount(word: string): number {
2+
let f = 1;
3+
for (let i = 1; i < word.length; ++i) {
4+
f += word[i] === word[i - 1] ? 1 : 0;
5+
}
6+
return f;
7+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /