Skip to content

Commit a523b6b

Browse files
committed
feat: add solutions to lc problem: No.3224
No.3324.Find the Sequence of Strings Appeared on the Screen
1 parent 107d978 commit a523b6b

File tree

7 files changed

+204
-8
lines changed

7 files changed

+204
-8
lines changed

solution/3300-3399/3324.Find the Sequence of Strings Appeared on the Screen/README.md

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,99 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3324.Fi
7373

7474
<!-- solution:start -->
7575

76-
### 方法一
76+
### 方法一:模拟
77+
78+
我们可以模拟 Alice 按键的过程,从空字符串开始,每次按键后更新字符串,直到得到目标字符串。
79+
80+
时间复杂度 $O(n^2 \times |\Sigma|)$,其中 $n$ 是目标字符串的长度,而 $\Sigma$ 是字符集,这里是小写字母集合,因此 $|\Sigma| = 26$。
7781

7882
<!-- tabs:start -->
7983

8084
#### Python3
8185

8286
```python
83-
87+
class Solution:
88+
def stringSequence(self, target: str) -> List[str]:
89+
ans = []
90+
for c in target:
91+
s = ans[-1] if ans else ""
92+
for a in ascii_lowercase:
93+
t = s + a
94+
ans.append(t)
95+
if a == c:
96+
break
97+
return ans
8498
```
8599

86100
#### Java
87101

88102
```java
89-
103+
class Solution {
104+
public List<String> stringSequence(String target) {
105+
List<String> ans = new ArrayList<>();
106+
for (char c : target.toCharArray()) {
107+
String s = ans.isEmpty() ? "" : ans.get(ans.size() - 1);
108+
for (char a = 'a'; a <= c; ++a) {
109+
String t = s + a;
110+
ans.add(t);
111+
}
112+
}
113+
return ans;
114+
}
115+
}
90116
```
91117

92118
#### C++
93119

94120
```cpp
95-
121+
class Solution {
122+
public:
123+
vector<string> stringSequence(string target) {
124+
vector<string> ans;
125+
for (char c : target) {
126+
string s = ans.empty() ? "" : ans.back();
127+
for (char a = 'a'; a <= c; ++a) {
128+
string t = s + a;
129+
ans.push_back(t);
130+
}
131+
}
132+
return ans;
133+
}
134+
};
96135
```
97136
98137
#### Go
99138
100139
```go
140+
func stringSequence(target string) (ans []string) {
141+
for _, c := range target {
142+
s := ""
143+
if len(ans) > 0 {
144+
s = ans[len(ans)-1]
145+
}
146+
for a := 'a'; a <= c; a++ {
147+
t := s + string(a)
148+
ans = append(ans, t)
149+
}
150+
}
151+
return
152+
}
153+
```
101154

155+
#### TypeScript
156+
157+
```ts
158+
function stringSequence(target: string): string[] {
159+
const ans: string[] = [];
160+
for (const c of target) {
161+
let s = ans.length > 0 ? ans[ans.length - 1] : '';
162+
for (let a = 'a'.charCodeAt(0); a <= c.charCodeAt(0); a++) {
163+
const t = s + String.fromCharCode(a);
164+
ans.push(t);
165+
}
166+
}
167+
return ans;
168+
}
102169
```
103170

104171
<!-- tabs:end -->

solution/3300-3399/3324.Find the Sequence of Strings Appeared on the Screen/README_EN.md

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,99 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3324.Fi
7171

7272
<!-- solution:start -->
7373

74-
### Solution 1
74+
### Solution 1: Simulation
75+
76+
We can simulate Alice's typing process, starting from an empty string and updating the string after each keystroke until the target string is obtained.
77+
78+
The time complexity is $O(n^2 \times |\Sigma|)$, where $n$ is the length of the target string and $\Sigma$ is the character set, which in this case is the set of lowercase letters, so $|\Sigma| = 26$.
7579

7680
<!-- tabs:start -->
7781

7882
#### Python3
7983

8084
```python
81-
85+
class Solution:
86+
def stringSequence(self, target: str) -> List[str]:
87+
ans = []
88+
for c in target:
89+
s = ans[-1] if ans else ""
90+
for a in ascii_lowercase:
91+
t = s + a
92+
ans.append(t)
93+
if a == c:
94+
break
95+
return ans
8296
```
8397

8498
#### Java
8599

86100
```java
87-
101+
class Solution {
102+
public List<String> stringSequence(String target) {
103+
List<String> ans = new ArrayList<>();
104+
for (char c : target.toCharArray()) {
105+
String s = ans.isEmpty() ? "" : ans.get(ans.size() - 1);
106+
for (char a = 'a'; a <= c; ++a) {
107+
String t = s + a;
108+
ans.add(t);
109+
}
110+
}
111+
return ans;
112+
}
113+
}
88114
```
89115

90116
#### C++
91117

92118
```cpp
93-
119+
class Solution {
120+
public:
121+
vector<string> stringSequence(string target) {
122+
vector<string> ans;
123+
for (char c : target) {
124+
string s = ans.empty() ? "" : ans.back();
125+
for (char a = 'a'; a <= c; ++a) {
126+
string t = s + a;
127+
ans.push_back(t);
128+
}
129+
}
130+
return ans;
131+
}
132+
};
94133
```
95134
96135
#### Go
97136
98137
```go
138+
func stringSequence(target string) (ans []string) {
139+
for _, c := range target {
140+
s := ""
141+
if len(ans) > 0 {
142+
s = ans[len(ans)-1]
143+
}
144+
for a := 'a'; a <= c; a++ {
145+
t := s + string(a)
146+
ans = append(ans, t)
147+
}
148+
}
149+
return
150+
}
151+
```
99152

153+
#### TypeScript
154+
155+
```ts
156+
function stringSequence(target: string): string[] {
157+
const ans: string[] = [];
158+
for (const c of target) {
159+
let s = ans.length > 0 ? ans[ans.length - 1] : '';
160+
for (let a = 'a'.charCodeAt(0); a <= c.charCodeAt(0); a++) {
161+
const t = s + String.fromCharCode(a);
162+
ans.push(t);
163+
}
164+
}
165+
return ans;
166+
}
100167
```
101168

102169
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
vector<string> stringSequence(string target) {
4+
vector<string> ans;
5+
for (char c : target) {
6+
string s = ans.empty() ? "" : ans.back();
7+
for (char a = 'a'; a <= c; ++a) {
8+
string t = s + a;
9+
ans.push_back(t);
10+
}
11+
}
12+
return ans;
13+
}
14+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func stringSequence(target string) (ans []string) {
2+
for _, c := range target {
3+
s := ""
4+
if len(ans) > 0 {
5+
s = ans[len(ans)-1]
6+
}
7+
for a := 'a'; a <= c; a++ {
8+
t := s + string(a)
9+
ans = append(ans, t)
10+
}
11+
}
12+
return
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public List<String> stringSequence(String target) {
3+
List<String> ans = new ArrayList<>();
4+
for (char c : target.toCharArray()) {
5+
String s = ans.isEmpty() ? "" : ans.get(ans.size() - 1);
6+
for (char a = 'a'; a <= c; ++a) {
7+
String t = s + a;
8+
ans.add(t);
9+
}
10+
}
11+
return ans;
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def stringSequence(self, target: str) -> List[str]:
3+
ans = []
4+
for c in target:
5+
s = ans[-1] if ans else ""
6+
for a in ascii_lowercase:
7+
t = s + a
8+
ans.append(t)
9+
if a == c:
10+
break
11+
return ans
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function stringSequence(target: string): string[] {
2+
const ans: string[] = [];
3+
for (const c of target) {
4+
let s = ans.length > 0 ? ans[ans.length - 1] : '';
5+
for (let a = 'a'.charCodeAt(0); a <= c.charCodeAt(0); a++) {
6+
const t = s + String.fromCharCode(a);
7+
ans.push(t);
8+
}
9+
}
10+
return ans;
11+
}

0 commit comments

Comments
 (0)