Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.1961~1964 #2145

Merged
merged 2 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ s 可以由 "i"、"love" 和 "leetcode" 相连得到。

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

**方法一:遍历**

我们遍历数组 $words$,用一个变量 $t$ 记录当前已经拼接的字符串,如果 $t$ 的长度大于 $s$ 的长度,说明 $s$ 不是 $words$ 的前缀字符串,返回 $false$;如果 $t$ 的长度等于 $s$ 的长度,返回 $t$ 是否等于 $s$。

遍历结束后,如果 $t$ 的长度小于 $s$ 的长度,说明 $s$ 不是 $words$ 的前缀字符串,返回 $false$。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。

<!-- tabs:start -->

### **Python3**
Expand All @@ -55,11 +63,11 @@ s 可以由 "i"、"love" 和 "leetcode" 相连得到。
```python
class Solution:
def isPrefixString(self, s: str, words: List[str]) -> bool:
t = 0
n, m = len(s), 0
for i, w in enumerate(words):
t += len(w)
if len(s) == t:
return ''.join(words[: i + 1]) == s
m += len(w)
if m == n:
return "".join(words[: i + 1]) == s
return False
```

Expand All @@ -71,9 +79,12 @@ class Solution:
class Solution {
public boolean isPrefixString(String s, String[] words) {
StringBuilder t = new StringBuilder();
for (String w : words) {
for (var w : words) {
t.append(w);
if (s.length() == t.length()) {
if (t.length() > s.length()) {
return false;
}
if (t.length() == s.length()) {
return s.equals(t.toString());
}
}
Expand All @@ -88,10 +99,15 @@ class Solution {
class Solution {
public:
bool isPrefixString(string s, vector<string>& words) {
string t = "";
for (string& w : words) {
string t;
for (auto& w : words) {
t += w;
if (t.size() == s.size()) return t == s;
if (t.size() > s.size()) {
return false;
}
if (t.size() == s.size()) {
return t == s;
}
}
return false;
}
Expand All @@ -102,17 +118,41 @@ public:

```go
func isPrefixString(s string, words []string) bool {
t := ""
t := strings.Builder{}
for _, w := range words {
t += w
if t == s {
return true
t.WriteString(w)
if t.Len() > len(s) {
return false
}
if t.Len() == len(s) {
return t.String() == s
}
}
return false
}
```

### **TypeScript**

```ts
function isPrefixString(s: string, words: string[]): boolean {
const t: string[] = [];
const n = s.length;
let m = 0;
for (const w of words) {
m += w.length;
if (m > n) {
return false;
}
t.push(w);
if (m === n) {
return s === t.join('');
}
}
return false;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,26 @@ It is impossible to make s using a prefix of arr.</pre>

## Solutions

**Solution 1: Traversal**

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$.

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$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def isPrefixString(self, s: str, words: List[str]) -> bool:
t = 0
n, m = len(s), 0
for i, w in enumerate(words):
t += len(w)
if len(s) == t:
return ''.join(words[: i + 1]) == s
m += len(w)
if m == n:
return "".join(words[: i + 1]) == s
return False
```

Expand All @@ -61,9 +69,12 @@ class Solution:
class Solution {
public boolean isPrefixString(String s, String[] words) {
StringBuilder t = new StringBuilder();
for (String w : words) {
for (var w : words) {
t.append(w);
if (s.length() == t.length()) {
if (t.length() > s.length()) {
return false;
}
if (t.length() == s.length()) {
return s.equals(t.toString());
}
}
Expand All @@ -78,10 +89,15 @@ class Solution {
class Solution {
public:
bool isPrefixString(string s, vector<string>& words) {
string t = "";
for (string& w : words) {
string t;
for (auto& w : words) {
t += w;
if (t.size() == s.size()) return t == s;
if (t.size() > s.size()) {
return false;
}
if (t.size() == s.size()) {
return t == s;
}
}
return false;
}
Expand All @@ -92,17 +108,41 @@ public:

```go
func isPrefixString(s string, words []string) bool {
t := ""
t := strings.Builder{}
for _, w := range words {
t += w
if t == s {
return true
t.WriteString(w)
if t.Len() > len(s) {
return false
}
if t.Len() == len(s) {
return t.String() == s
}
}
return false
}
```

### **TypeScript**

```ts
function isPrefixString(s: string, words: string[]): boolean {
const t: string[] = [];
const n = s.length;
let m = 0;
for (const w of words) {
m += w.length;
if (m > n) {
return false;
}
t.push(w);
if (m === n) {
return s === t.join('');
}
}
return false;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
class Solution {
public:
bool isPrefixString(string s, vector<string>& words) {
string t = "";
for (string& w : words) {
t += w;
if (t.size() == s.size()) return t == s;
}
return false;
}
class Solution {
public:
bool isPrefixString(string s, vector<string>& words) {
string t;
for (auto& w : words) {
t += w;
if (t.size() > s.size()) {
return false;
}
if (t.size() == s.size()) {
return t == s;
}
}
return false;
}
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
func isPrefixString(s string, words []string) bool {
t := ""
t := strings.Builder{}
for _, w := range words {
t += w
if t == s {
return true
t.WriteString(w)
if t.Len() > len(s) {
return false
}
if t.Len() == len(s) {
return t.String() == s
}
}
return false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
class Solution {
public boolean isPrefixString(String s, String[] words) {
StringBuilder t = new StringBuilder();
for (String w : words) {
t.append(w);
if (s.length() == t.length()) {
return s.equals(t.toString());
}
}
return false;
}
class Solution {
public boolean isPrefixString(String s, String[] words) {
StringBuilder t = new StringBuilder();
for (var w : words) {
t.append(w);
if (t.length() > s.length()) {
return false;
}
if (t.length() == s.length()) {
return s.equals(t.toString());
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Solution:
def isPrefixString(self, s: str, words: List[str]) -> bool:
t = 0
for i, w in enumerate(words):
t += len(w)
if len(s) == t:
return ''.join(words[: i + 1]) == s
return False
class Solution:
def isPrefixString(self, s: str, words: List[str]) -> bool:
n, m = len(s), 0
for i, w in enumerate(words):
m += len(w)
if m == n:
return "".join(words[: i + 1]) == s
return False
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function isPrefixString(s: string, words: string[]): boolean {
const t: string[] = [];
const n = s.length;
let m = 0;
for (const w of words) {
m += w.length;
if (m > n) {
return false;
}
t.push(w);
if (m === n) {
return s === t.join('');
}
}
return false;
}
Loading