Skip to content

feat: add solutions to lc problems: No.524,525 #3116

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

Merged
merged 1 commit into from
Jun 17, 2024
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 @@ -56,7 +56,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:判断子序列

我们定义一个函数 $check(s, t)$,用于判断字符串 $s$ 是否是字符串 $t$ 的子序列。我们可以使用双指针的方法,初始化两个指针 $i$ 和 $j$ 分别指向字符串 $s$ 和字符串 $t$ 的开头,然后不断移动指针 $j$,如果 $s[i]$ 和 $t[j]$ 相等,则移动指针 $i$,最后判断 $i$ 是否等于 $s$ 的长度即可。若 $i$ 等于 $s$ 的长度,则说明 $s$ 是 $t$ 的子序列。

我们初始化答案字符串 $ans$ 为空字符串,然后遍历数组 $dictionary$ 中的每个字符串 $t$,如果 $t$ 是 $s$ 的子序列,并且 $t$ 的长度大于 $ans$ 的长度,或者 $t$ 的长度等于 $ans$ 的长度且 $t$ 字典序小于 $ans$,则更新 $ans$ 为 $t$。

时间复杂度 $O(d \times (m + n))$,其中 $d$ 是字符串列表的长度,而 $m$ 和 $n$ 分别是字符串 $s$ 的长度和字符串列表中字符串的平均长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -65,19 +71,19 @@ tags:
```python
class Solution:
def findLongestWord(self, s: str, dictionary: List[str]) -> str:
def check(a, b):
m, n = len(a), len(b)
def check(s: str, t: str) -> bool:
m, n = len(s), len(t)
i = j = 0
while i < m and j < n:
if a[i] == b[j]:
j += 1
i += 1
return j == n

ans = ''
for a in dictionary:
if check(s, a) and (len(ans) < len(a) or (len(ans) == len(a) and ans > a)):
ans = a
if s[i] == t[j]:
i += 1
j += 1
return i == m

ans = ""
for t in dictionary:
if check(t, s) and (len(ans) < len(t) or (len(ans) == len(t) and ans > t)):
ans = t
return ans
```

Expand All @@ -87,26 +93,24 @@ class Solution:
class Solution {
public String findLongestWord(String s, List<String> dictionary) {
String ans = "";
for (String a : dictionary) {
if (check(s, a)
&& (ans.length() < a.length()
|| (ans.length() == a.length() && a.compareTo(ans) < 0))) {
ans = a;
for (String t : dictionary) {
int a = ans.length(), b = t.length();
if (check(t, s) && (a < b || (a == b && t.compareTo(ans) < 0))) {
ans = t;
}
}
return ans;
}

private boolean check(String a, String b) {
int m = a.length(), n = b.length();
int i = 0, j = 0;
while (i < m && j < n) {
if (a.charAt(i) == b.charAt(j)) {
++j;
private boolean check(String s, String t) {
int m = s.length(), n = t.length();
int i = 0;
for (int j = 0; i < m && j < n; ++j) {
if (s.charAt(i) == t.charAt(j)) {
++i;
}
++i;
}
return j == n;
return i == m;
}
}
```
Expand All @@ -118,20 +122,23 @@ class Solution {
public:
string findLongestWord(string s, vector<string>& dictionary) {
string ans = "";
for (string& a : dictionary)
if (check(s, a) && (ans.size() < a.size() || (ans.size() == a.size() && a < ans)))
ans = a;
return ans;
}

bool check(string& a, string& b) {
int m = a.size(), n = b.size();
int i = 0, j = 0;
while (i < m && j < n) {
if (a[i] == b[j]) ++j;
++i;
auto check = [&](const string& s, const string& t) {
int m = s.size(), n = t.size();
int i = 0;
for (int j = 0; i < m && j < n; ++j) {
if (s[i] == t[j]) {
++i;
}
}
return i == m;
};
for (auto& t : dictionary) {
int a = ans.size(), b = t.size();
if (check(t, s) && (a < b || (a == b && ans > t))) {
ans = t;
}
}
return j == n;
return ans;
}
};
```
Expand All @@ -140,21 +147,21 @@ public:

```go
func findLongestWord(s string, dictionary []string) string {
ans := ""
check := func(a, b string) bool {
m, n := len(a), len(b)
i, j := 0, 0
for i < m && j < n {
if a[i] == b[j] {
j++
ans := ''
check := func(s, t string) bool {
m, n := len(s), len(t)
i := 0
for j := 0; i < m && j < n; j++ {
if s[i] == t[j] {
i++
}
i++
}
return j == n
return i == m
}
for _, a := range dictionary {
if check(s, a) && (len(ans) < len(a) || (len(ans) == len(a) && a < ans)) {
ans = a
for _, t := range dictionary {
a, b := len(ans), len(t)
if check(t, s) && (a < b || (a == b && ans > t)) {
ans = t
}
}
return ans
Expand All @@ -165,57 +172,57 @@ func findLongestWord(s string, dictionary []string) string {

```ts
function findLongestWord(s: string, dictionary: string[]): string {
dictionary.sort((a, b) => {
if (a.length === b.length) {
return b < a ? 1 : -1;
}
return b.length - a.length;
});
const n = s.length;
for (const target of dictionary) {
const m = target.length;
if (m > n) {
continue;
}
const check = (s: string, t: string): boolean => {
const [m, n] = [s.length, t.length];
let i = 0;
let j = 0;
while (i < n && j < m) {
if (s[i] === target[j]) {
j++;
for (let j = 0; i < m && j < n; ++j) {
if (s[i] === t[j]) {
++i;
}
i++;
}
if (j === m) {
return target;
return i === m;
};
let ans: string = '';
for (const t of dictionary) {
const [a, b] = [ans.length, t.length];
if (check(t, s) && (a < b || (a === b && ans > t))) {
ans = t;
}
}
return '';
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn find_longest_word(s: String, mut dictionary: Vec<String>) -> String {
dictionary.sort_unstable_by(|a, b| (b.len(), a).cmp(&(a.len(), b)));
for target in dictionary {
let target: Vec<char> = target.chars().collect();
let n = target.len();
let mut i = 0;
for c in s.chars() {
if i == n {
break;
}
if c == target[i] {
i += 1;
}
pub fn find_longest_word(s: String, dictionary: Vec<String>) -> String {
let mut ans = String::new();
for t in dictionary {
let a = ans.len();
let b = t.len();
if Self::check(&t, &s) && (a < b || (a == b && t < ans)) {
ans = t;
}
if i == n {
return target.iter().collect();
}
ans
}

fn check(s: &str, t: &str) -> bool {
let (m, n) = (s.len(), t.len());
let mut i = 0;
let mut j = 0;
let s: Vec<char> = s.chars().collect();
let t: Vec<char> = t.chars().collect();

while i < m && j < n {
if s[i] == t[j] {
i += 1;
}
j += 1;
}
String::new()
i == m
}
}
```
Expand Down
Loading