|
1 | 1 | //go:build ignore |
2 | 2 | #include "cpp/common/Solution.h" |
3 | | - |
| 3 | +#include <ranges> |
| 4 | +#include <string_view> |
4 | 5 |
|
5 | 6 | using namespace std; |
6 | 7 | using json = nlohmann::json; |
7 | 8 |
|
8 | 9 | class Solution { |
9 | 10 | 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 | + } |
12 | 20 | } |
| 21 | + return ans; |
| 22 | + } |
13 | 23 | }; |
14 | 24 |
|
15 | 25 | 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); |
24 | 34 |
|
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); |
29 | 39 | } |
0 commit comments