File tree 6 files changed +144
-2
lines changed
solution/1900-1999/1961.Check If String Is a Prefix of Array
6 files changed +144
-2
lines changed Original file line number Diff line number Diff line change @@ -53,15 +53,65 @@ s 可以由 "i"、"love" 和 "leetcode" 相连得到。
53
53
<!-- 这里可写当前语言的特殊实现逻辑 -->
54
54
55
55
``` 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
57
64
```
58
65
59
66
### ** Java**
60
67
61
68
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
69
63
70
``` 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
+ ```
64
101
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
+ }
65
115
```
66
116
67
117
### ** ...**
Original file line number Diff line number Diff line change @@ -45,13 +45,63 @@ It is impossible to make s using a prefix of arr.</pre>
45
45
### ** Python3**
46
46
47
47
``` 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
49
56
```
50
57
51
58
### ** Java**
52
59
53
60
``` 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
+ ```
54
91
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
+ }
55
105
```
56
106
57
107
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments