Skip to content

feat: add solutions to lc problems: No.1935,1936 #1844

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
Oct 19, 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
150 changes: 128 additions & 22 deletions solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@

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

**方法一:数组或哈希表**

我们可以用哈希表或者一个长度为 $26$ 的数组 $s$ 来记录所有损坏的字母键。

然后,我们遍历字符串 $text$ 中的每个单词 $w$,如果 $w$ 中的某个字母 $c$ 在 $s$ 中出现过,那么说明这个单词无法输入,答案不需要加一,否则答案需要加一。

遍历结束后,返回答案即可。

时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$,其中 $n$ 是字符串 $text$ 的长度,而 $|\Sigma|$ 是字母表的大小,本题中 $|\Sigma|=26$。

<!-- tabs:start -->

### **Python3**
Expand All @@ -58,17 +68,8 @@
```python
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
letters = set(brokenLetters)
res = 0
for word in text.split():
find = False
for letter in letters:
if letter in word:
find = True
break
if not find:
res += 1
return res
s = set(brokenLetters)
return sum(all(c not in s for c in w) for w in text.split())
```

### **Java**
Expand All @@ -78,24 +79,129 @@ class Solution:
```java
class Solution {
public int canBeTypedWords(String text, String brokenLetters) {
Set<Character> letters = new HashSet<>();
boolean[] s = new boolean[26];
for (char c : brokenLetters.toCharArray()) {
letters.add(c);
s[c - 'a'] = true;
}
int ans = 0;
for (String w : text.split(" ")) {
for (char c : w.toCharArray()) {
if (s[c - 'a']) {
--ans;
break;
}
}
++ans;
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int canBeTypedWords(string text, string brokenLetters) {
bool s[26]{};
for (char& c : brokenLetters) {
s[c - 'a'] = true;
}
int res = 0;
for (String word : text.split(" ")) {
boolean find = false;
for (char c : letters) {
if (word.indexOf(c) > -1) {
find = true;
int ans = 0;
for (auto& w : split(text, ' ')) {
for (char& c : w) {
if (s[c - 'a']) {
--ans;
break;
}
}
if (!find) {
++res;
++ans;
}
return ans;
}

vector<string> split(const string& s, char c) {
vector<string> ans;
string t;
for (char d : s) {
if (d == c) {
ans.push_back(t);
t.clear();
} else {
t.push_back(d);
}
}
ans.push_back(t);
return ans;
}
};
```

### **Go**

```go
func canBeTypedWords(text string, brokenLetters string) (ans int) {
s := [26]bool{}
for _, c := range brokenLetters {
s[c-'a'] = true
}
for _, w := range strings.Split(text, " ") {
for _, c := range w {
if s[c-'a'] {
ans--
break
}
}
ans++
}
return
}
```

### **TypeScript**

```ts
function canBeTypedWords(text: string, brokenLetters: string): number {
const s: boolean[] = Array(26).fill(false);
for (const c of brokenLetters) {
s[c.charCodeAt(0) - 'a'.charCodeAt(0)] = true;
}
let ans = 0;
for (const w of text.split(' ')) {
for (const c of w) {
if (s[c.charCodeAt(0) - 'a'.charCodeAt(0)]) {
--ans;
break;
}
}
++ans;
}
return ans;
}
```

### **Rust**

```rust
impl Solution {
pub fn can_be_typed_words(text: String, broken_letters: String) -> i32 {
let mut s = vec![false; 26];
for c in broken_letters.chars() {
s[c as usize - 'a' as usize] = true;
}
let mut ans = 0;
let words = text.split_whitespace();
for w in words {
for c in w.chars() {
if s[c as usize - 'a' as usize] {
ans -= 1;
break;
}
}
ans += 1;
}
return res;
ans
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,49 +46,155 @@

## Solutions

**Method 1: Array or Hash Table**

We can use a hash table or an array $s$ of length $26$ to record all the broken letter keys.

Then, we traverse each word $w$ in the string $text$, and if any letter $c$ in $w$ appears in $s$, it means that the word cannot be typed, and we do not need to add one to the answer. Otherwise, we need to add one to the answer.

After the traversal, we return the answer.

The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$, where $n$ is the length of the string $text$, and $|\Sigma|$ is the size of the alphabet. In this problem, $|\Sigma|=26$.

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
letters = set(brokenLetters)
res = 0
for word in text.split():
find = False
for letter in letters:
if letter in word:
find = True
break
if not find:
res += 1
return res
s = set(brokenLetters)
return sum(all(c not in s for c in w) for w in text.split())
```

### **Java**

```java
class Solution {
public int canBeTypedWords(String text, String brokenLetters) {
Set<Character> letters = new HashSet<>();
boolean[] s = new boolean[26];
for (char c : brokenLetters.toCharArray()) {
letters.add(c);
s[c - 'a'] = true;
}
int ans = 0;
for (String w : text.split(" ")) {
for (char c : w.toCharArray()) {
if (s[c - 'a']) {
--ans;
break;
}
}
++ans;
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int canBeTypedWords(string text, string brokenLetters) {
bool s[26]{};
for (char& c : brokenLetters) {
s[c - 'a'] = true;
}
int res = 0;
for (String word : text.split(" ")) {
boolean find = false;
for (char c : letters) {
if (word.indexOf(c) > -1) {
find = true;
int ans = 0;
for (auto& w : split(text, ' ')) {
for (char& c : w) {
if (s[c - 'a']) {
--ans;
break;
}
}
if (!find) {
++res;
++ans;
}
return ans;
}

vector<string> split(const string& s, char c) {
vector<string> ans;
string t;
for (char d : s) {
if (d == c) {
ans.push_back(t);
t.clear();
} else {
t.push_back(d);
}
}
ans.push_back(t);
return ans;
}
};
```

### **Go**

```go
func canBeTypedWords(text string, brokenLetters string) (ans int) {
s := [26]bool{}
for _, c := range brokenLetters {
s[c-'a'] = true
}
for _, w := range strings.Split(text, " ") {
for _, c := range w {
if s[c-'a'] {
ans--
break
}
}
ans++
}
return
}
```

### **TypeScript**

```ts
function canBeTypedWords(text: string, brokenLetters: string): number {
const s: boolean[] = Array(26).fill(false);
for (const c of brokenLetters) {
s[c.charCodeAt(0) - 'a'.charCodeAt(0)] = true;
}
let ans = 0;
for (const w of text.split(' ')) {
for (const c of w) {
if (s[c.charCodeAt(0) - 'a'.charCodeAt(0)]) {
--ans;
break;
}
}
++ans;
}
return ans;
}
```

### **Rust**

```rust
impl Solution {
pub fn can_be_typed_words(text: String, broken_letters: String) -> i32 {
let mut s = vec![false; 26];
for c in broken_letters.chars() {
s[c as usize - 'a' as usize] = true;
}
let mut ans = 0;
let words = text.split_whitespace();
for w in words {
for c in w.chars() {
if s[c as usize - 'a' as usize] {
ans -= 1;
break;
}
}
ans += 1;
}
return res;
ans
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
int canBeTypedWords(string text, string brokenLetters) {
bool s[26]{};
for (char& c : brokenLetters) {
s[c - 'a'] = true;
}
int ans = 0;
for (auto& w : split(text, ' ')) {
for (char& c : w) {
if (s[c - 'a']) {
--ans;
break;
}
}
++ans;
}
return ans;
}

vector<string> split(const string& s, char c) {
vector<string> ans;
string t;
for (char d : s) {
if (d == c) {
ans.push_back(t);
t.clear();
} else {
t.push_back(d);
}
}
ans.push_back(t);
return ans;
}
};
Loading