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 3b2e551

Browse files
committed
feat: add solutions to lc problem: No.1961
No.1961.Check If String Is a Prefix of Array
1 parent 1c88423 commit 3b2e551

File tree

6 files changed

+144
-2
lines changed

6 files changed

+144
-2
lines changed

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,65 @@ s 可以由 "i"、"love" 和 "leetcode" 相连得到。
5353
<!-- 这里可写当前语言的特殊实现逻辑 -->
5454

5555
```python
56-
56+
class Solution:
57+
def isPrefixString(self, s: str, words: List[str]) -> bool:
58+
t = 0
59+
for i, w in enumerate(words):
60+
t += len(w)
61+
if len(s) == t:
62+
return ''.join(words[:i + 1]) == s
63+
return False
5764
```
5865

5966
### **Java**
6067

6168
<!-- 这里可写当前语言的特殊实现逻辑 -->
6269

6370
```java
71+
class Solution {
72+
public boolean isPrefixString(String s, String[] words) {
73+
StringBuilder t = new StringBuilder();
74+
for (String w : words) {
75+
t.append(w);
76+
if (s.length() == t.length()) {
77+
return s.equals(t.toString());
78+
}
79+
}
80+
return false;
81+
}
82+
}
83+
```
84+
85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
bool isPrefixString(string s, vector<string>& words) {
91+
string t = "";
92+
for (string& w : words)
93+
{
94+
t += w;
95+
if (t.size() == s.size()) return t == s;
96+
}
97+
return false;
98+
}
99+
};
100+
```
64101
102+
### **Go**
103+
104+
```go
105+
func isPrefixString(s string, words []string) bool {
106+
t := ""
107+
for _, w := range words {
108+
t += w
109+
if t == s {
110+
return true
111+
}
112+
}
113+
return false
114+
}
65115
```
66116

67117
### **...**

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,63 @@ It is impossible to make s using a prefix of arr.</pre>
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def isPrefixString(self, s: str, words: List[str]) -> bool:
50+
t = 0
51+
for i, w in enumerate(words):
52+
t += len(w)
53+
if len(s) == t:
54+
return ''.join(words[:i + 1]) == s
55+
return False
4956
```
5057

5158
### **Java**
5259

5360
```java
61+
class Solution {
62+
public boolean isPrefixString(String s, String[] words) {
63+
StringBuilder t = new StringBuilder();
64+
for (String w : words) {
65+
t.append(w);
66+
if (s.length() == t.length()) {
67+
return s.equals(t.toString());
68+
}
69+
}
70+
return false;
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
bool isPrefixString(string s, vector<string>& words) {
81+
string t = "";
82+
for (string& w : words)
83+
{
84+
t += w;
85+
if (t.size() == s.size()) return t == s;
86+
}
87+
return false;
88+
}
89+
};
90+
```
5491
92+
### **Go**
93+
94+
```go
95+
func isPrefixString(s string, words []string) bool {
96+
t := ""
97+
for _, w := range words {
98+
t += w
99+
if t == s {
100+
return true
101+
}
102+
}
103+
return false
104+
}
55105
```
56106

57107
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
bool isPrefixString(string s, vector<string>& words) {
4+
string t = "";
5+
for (string& w : words)
6+
{
7+
t += w;
8+
if (t.size() == s.size()) return t == s;
9+
}
10+
return false;
11+
}
12+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func isPrefixString(s string, words []string) bool {
2+
t := ""
3+
for _, w := range words {
4+
t += w
5+
if t == s {
6+
return true
7+
}
8+
}
9+
return false
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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

0 commit comments

Comments
(0)

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