Skip to content

Commit 87363bc

Browse files
authored
feat: add solutions to lc problems: No.1961~1964 (doocs#2145)
* No.1961.Check If String Is a Prefix of Array * No.1963.Minimum Number of Swaps to Make the String Balanced * No.1964.Find the Longest Valid Obstacle Course at Each Position
1 parent 3d8a308 commit 87363bc

File tree

21 files changed

+783
-565
lines changed

21 files changed

+783
-565
lines changed

solution/1900-1999/1961.Check If String Is a Prefix of Array/README.md

+53-13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ s 可以由 "i"、"love" 和 "leetcode" 相连得到。
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:遍历**
50+
51+
我们遍历数组 $words$,用一个变量 $t$ 记录当前已经拼接的字符串,如果 $t$ 的长度大于 $s$ 的长度,说明 $s$ 不是 $words$ 的前缀字符串,返回 $false$;如果 $t$ 的长度等于 $s$ 的长度,返回 $t$ 是否等于 $s$。
52+
53+
遍历结束后,如果 $t$ 的长度小于 $s$ 的长度,说明 $s$ 不是 $words$ 的前缀字符串,返回 $false$。
54+
55+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
56+
4957
<!-- tabs:start -->
5058

5159
### **Python3**
@@ -55,11 +63,11 @@ s 可以由 "i"、"love" 和 "leetcode" 相连得到。
5563
```python
5664
class Solution:
5765
def isPrefixString(self, s: str, words: List[str]) -> bool:
58-
t = 0
66+
n, m = len(s), 0
5967
for i, w in enumerate(words):
60-
t += len(w)
61-
if len(s) == t:
62-
return ''.join(words[: i + 1]) == s
68+
m += len(w)
69+
if m == n:
70+
return "".join(words[: i + 1]) == s
6371
return False
6472
```
6573

@@ -71,9 +79,12 @@ class Solution:
7179
class Solution {
7280
public boolean isPrefixString(String s, String[] words) {
7381
StringBuilder t = new StringBuilder();
74-
for (String w : words) {
82+
for (var w : words) {
7583
t.append(w);
76-
if (s.length() == t.length()) {
84+
if (t.length() > s.length()) {
85+
return false;
86+
}
87+
if (t.length() == s.length()) {
7788
return s.equals(t.toString());
7889
}
7990
}
@@ -88,10 +99,15 @@ class Solution {
8899
class Solution {
89100
public:
90101
bool isPrefixString(string s, vector<string>& words) {
91-
string t = "";
92-
for (string& w : words) {
102+
string t;
103+
for (auto& w : words) {
93104
t += w;
94-
if (t.size() == s.size()) return t == s;
105+
if (t.size() > s.size()) {
106+
return false;
107+
}
108+
if (t.size() == s.size()) {
109+
return t == s;
110+
}
95111
}
96112
return false;
97113
}
@@ -102,17 +118,41 @@ public:
102118
103119
```go
104120
func isPrefixString(s string, words []string) bool {
105-
t := ""
121+
t := strings.Builder{}
106122
for _, w := range words {
107-
t += w
108-
if t == s {
109-
return true
123+
t.WriteString(w)
124+
if t.Len() > len(s) {
125+
return false
126+
}
127+
if t.Len() == len(s) {
128+
return t.String() == s
110129
}
111130
}
112131
return false
113132
}
114133
```
115134

135+
### **TypeScript**
136+
137+
```ts
138+
function isPrefixString(s: string, words: string[]): boolean {
139+
const t: string[] = [];
140+
const n = s.length;
141+
let m = 0;
142+
for (const w of words) {
143+
m += w.length;
144+
if (m > n) {
145+
return false;
146+
}
147+
t.push(w);
148+
if (m === n) {
149+
return s === t.join('');
150+
}
151+
}
152+
return false;
153+
}
154+
```
155+
116156
### **...**
117157

118158
```

solution/1900-1999/1961.Check If String Is a Prefix of Array/README_EN.md

+53-13
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,26 @@ It is impossible to make s using a prefix of arr.</pre>
4040

4141
## Solutions
4242

43+
**Solution 1: Traversal**
44+
45+
We traverse the array $words$, using a variable $t$ to record the currently concatenated string. If the length of $t$ is greater than the length of $s$, it means that $s$ is not a prefix string of $words$, so we return $false$; if the length of $t$ is equal to the length of $s$, we return whether $t$ is equal to $s$.
46+
47+
At the end of the traversal, if the length of $t$ is less than the length of $s$, it means that $s$ is not a prefix string of $words$, so we return $false$.
48+
49+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
50+
4351
<!-- tabs:start -->
4452

4553
### **Python3**
4654

4755
```python
4856
class Solution:
4957
def isPrefixString(self, s: str, words: List[str]) -> bool:
50-
t = 0
58+
n, m = len(s), 0
5159
for i, w in enumerate(words):
52-
t += len(w)
53-
if len(s) == t:
54-
return ''.join(words[: i + 1]) == s
60+
m += len(w)
61+
if m == n:
62+
return "".join(words[: i + 1]) == s
5563
return False
5664
```
5765

@@ -61,9 +69,12 @@ class Solution:
6169
class Solution {
6270
public boolean isPrefixString(String s, String[] words) {
6371
StringBuilder t = new StringBuilder();
64-
for (String w : words) {
72+
for (var w : words) {
6573
t.append(w);
66-
if (s.length() == t.length()) {
74+
if (t.length() > s.length()) {
75+
return false;
76+
}
77+
if (t.length() == s.length()) {
6778
return s.equals(t.toString());
6879
}
6980
}
@@ -78,10 +89,15 @@ class Solution {
7889
class Solution {
7990
public:
8091
bool isPrefixString(string s, vector<string>& words) {
81-
string t = "";
82-
for (string& w : words) {
92+
string t;
93+
for (auto& w : words) {
8394
t += w;
84-
if (t.size() == s.size()) return t == s;
95+
if (t.size() > s.size()) {
96+
return false;
97+
}
98+
if (t.size() == s.size()) {
99+
return t == s;
100+
}
85101
}
86102
return false;
87103
}
@@ -92,17 +108,41 @@ public:
92108
93109
```go
94110
func isPrefixString(s string, words []string) bool {
95-
t := ""
111+
t := strings.Builder{}
96112
for _, w := range words {
97-
t += w
98-
if t == s {
99-
return true
113+
t.WriteString(w)
114+
if t.Len() > len(s) {
115+
return false
116+
}
117+
if t.Len() == len(s) {
118+
return t.String() == s
100119
}
101120
}
102121
return false
103122
}
104123
```
105124

125+
### **TypeScript**
126+
127+
```ts
128+
function isPrefixString(s: string, words: string[]): boolean {
129+
const t: string[] = [];
130+
const n = s.length;
131+
let m = 0;
132+
for (const w of words) {
133+
m += w.length;
134+
if (m > n) {
135+
return false;
136+
}
137+
t.push(w);
138+
if (m === n) {
139+
return s === t.join('');
140+
}
141+
}
142+
return false;
143+
}
144+
```
145+
106146
### **...**
107147

108148
```
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
class Solution {
2-
public:
3-
bool isPrefixString(string s, vector<string>& words) {
4-
string t = "";
5-
for (string& w : words) {
6-
t += w;
7-
if (t.size() == s.size()) return t == s;
8-
}
9-
return false;
10-
}
1+
class Solution {
2+
public:
3+
bool isPrefixString(string s, vector<string>& words) {
4+
string t;
5+
for (auto& w : words) {
6+
t += w;
7+
if (t.size() > s.size()) {
8+
return false;
9+
}
10+
if (t.size() == s.size()) {
11+
return t == s;
12+
}
13+
}
14+
return false;
15+
}
1116
};

solution/1900-1999/1961.Check If String Is a Prefix of Array/Solution.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
func isPrefixString(s string, words []string) bool {
2-
t := ""
2+
t := strings.Builder{}
33
for _, w := range words {
4-
t += w
5-
if t == s {
6-
return true
4+
t.WriteString(w)
5+
if t.Len() > len(s) {
6+
return false
7+
}
8+
if t.Len() == len(s) {
9+
return t.String() == s
710
}
811
}
912
return false
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
class Solution {
2-
public boolean isPrefixString(String s, String[] words) {
3-
StringBuilder t = new StringBuilder();
4-
for (String w : words) {
5-
t.append(w);
6-
if (s.length() == t.length()) {
7-
return s.equals(t.toString());
8-
}
9-
}
10-
return false;
11-
}
1+
class Solution {
2+
public boolean isPrefixString(String s, String[] words) {
3+
StringBuilder t = new StringBuilder();
4+
for (var w : words) {
5+
t.append(w);
6+
if (t.length() > s.length()) {
7+
return false;
8+
}
9+
if (t.length() == s.length()) {
10+
return s.equals(t.toString());
11+
}
12+
}
13+
return false;
14+
}
1215
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
class Solution:
2-
def isPrefixString(self, s: str, words: List[str]) -> bool:
3-
t = 0
4-
for i, w in enumerate(words):
5-
t += len(w)
6-
if len(s) == t:
7-
return ''.join(words[: i + 1]) == s
8-
return False
1+
class Solution:
2+
def isPrefixString(self, s: str, words: List[str]) -> bool:
3+
n, m = len(s), 0
4+
for i, w in enumerate(words):
5+
m += len(w)
6+
if m == n:
7+
return "".join(words[: i + 1]) == s
8+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function isPrefixString(s: string, words: string[]): boolean {
2+
const t: string[] = [];
3+
const n = s.length;
4+
let m = 0;
5+
for (const w of words) {
6+
m += w.length;
7+
if (m > n) {
8+
return false;
9+
}
10+
t.push(w);
11+
if (m === n) {
12+
return s === t.join('');
13+
}
14+
}
15+
return false;
16+
}

0 commit comments

Comments
 (0)