Skip to content

Commit 974fbd5

Browse files
committed
test: 1935 solution
py, c++, go, java
1 parent e5fc518 commit 974fbd5

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed
Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
11
//go:build ignore
22
#include "cpp/common/Solution.h"
3-
3+
#include <ranges>
4+
#include <string_view>
45

56
using namespace std;
67
using json = nlohmann::json;
78

89
class Solution {
910
public:
10-
int canBeTypedWords(string text, string brokenLetters) {
11-
11+
int canBeTypedWords(const string &text, const string &brokenLetters) {
12+
int ans = 0;
13+
std::string_view words{text}, delimiters{" "};
14+
for (const auto &t : std::ranges::split_view(words, delimiters)) {
15+
if (std::all_of(t.begin(), t.end(), [&brokenLetters](const char &c) -> bool {
16+
return brokenLetters.find(c) == std::string::npos;
17+
})) {
18+
++ans;
19+
}
1220
}
21+
return ans;
22+
}
1323
};
1424

1525
json leetcode::qubh::Solve(string input_json_values) {
16-
vector<string> inputArray;
17-
size_t pos = input_json_values.find('\n');
18-
while (pos != string::npos) {
19-
inputArray.push_back(input_json_values.substr(0, pos));
20-
input_json_values = input_json_values.substr(pos + 1);
21-
pos = input_json_values.find('\n');
22-
}
23-
inputArray.push_back(input_json_values);
26+
vector<string> inputArray;
27+
size_t pos = input_json_values.find('\n');
28+
while (pos != string::npos) {
29+
inputArray.push_back(input_json_values.substr(0, pos));
30+
input_json_values = input_json_values.substr(pos + 1);
31+
pos = input_json_values.find('\n');
32+
}
33+
inputArray.push_back(input_json_values);
2434

25-
Solution solution;
26-
string text = json::parse(inputArray.at(0));
27-
string brokenLetters = json::parse(inputArray.at(1));
28-
return solution.canBeTypedWords(text, brokenLetters);
35+
Solution solution;
36+
string text = json::parse(inputArray.at(0));
37+
string brokenLetters = json::parse(inputArray.at(1));
38+
return solution.canBeTypedWords(text, brokenLetters);
2939
}

problems/problems_1935/Solution.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@
77

88
public class Solution extends BaseSolution {
99
public int canBeTypedWords(String text, String brokenLetters) {
10-
10+
int brokenMask = 0;
11+
for (char c : brokenLetters.toCharArray()) {
12+
brokenMask |= 1 << (c - 'a'); // 把 c 加到集合中
13+
}
14+
15+
int ans = 0;
16+
int ok = 1;
17+
for (char c : text.toCharArray()) {
18+
if (c == ' ') { // 上一个单词遍历完毕
19+
ans += ok;
20+
ok = 1;
21+
} else if ((brokenMask >> (c - 'a') & 1) > 0) { // c 在 brokenLetters 中
22+
ok = 0;
23+
}
24+
}
25+
ans += ok; // 最后一个单词
26+
return ans;
1127
}
1228

1329
@Override

problems/problems_1935/solution.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ import (
66
"strings"
77
)
88

9-
func canBeTypedWords(text string, brokenLetters string) int {
10-
9+
func canBeTypedWords(text string, brokenLetters string) (ans int) {
10+
for _, t := range strings.Split(text, " ") {
11+
if !strings.ContainsAny(t, brokenLetters) {
12+
ans++
13+
}
14+
}
15+
return
1116
}
1217

1318
func Solve(inputJsonValues string) any {

0 commit comments

Comments
 (0)