Skip to content

feat: add solutions to lc problem: No.2047 #3368

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
Aug 6, 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
205 changes: 144 additions & 61 deletions solution/2000-2099/2047.Number of Valid Words in a Sentence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,25 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们首先将句子按空格分割成单词,然后对每个单词进行检查,判断是否为有效单词。

对于每个单词,我们可以使用一个布尔变量 $\textit{st}$ 来记录是否已经出现过连字符,然后遍历单词中的每个字符,根据题目描述的规则进行判断。

对于每个字符 $s[i]$,我们有以下几种情况:

- 如果 $s[i]$ 是数字,那么 $s$ 不是有效单词,直接返回 $\text{false}$;
- 如果 $s[i]$ 是标点符号('!'、'.'、',')且 $i < \text{len}(s) - 1$,那么 $s$ 不是有效单词,直接返回 $\text{false}$;
- 如果 $s[i]$ 是连字符,那么我们需要判断是否满足以下条件:
- 连字符只能出现一次;
- 连字符不能出现在单词的开头或结尾;
- 连字符两侧必须是字母;
- 如果 $s[i]$ 是字母,那么我们不需要做任何处理。

最后,我们统计出句子中的有效单词数即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是句子的长度。

<!-- tabs:start -->

Expand All @@ -86,24 +104,23 @@ tags:
```python
class Solution:
def countValidWords(self, sentence: str) -> int:
def check(token):
hyphen = False
for i, c in enumerate(token):
if c.isdigit() or (c in '!.,' and i < len(token) - 1):
def check(s: str) -> bool:
st = False
for i, c in enumerate(s):
if c.isdigit() or (c in "!.," and i < len(s) - 1):
return False
if c == '-':
if c == "-":
if (
hyphen
or i == 0
or i == len(token) - 1
or not token[i - 1].islower()
or not token[i + 1].islower()
st
or i in (0, len(s) - 1)
or not s[i - 1].isalpha()
or not s[i + 1].isalpha()
):
return False
hyphen = True
st = True
return True

return sum(check(token) for token in sentence.split())
return sum(check(s) for s in sentence.split())
```

#### Java
Expand All @@ -112,76 +129,142 @@ class Solution:
class Solution {
public int countValidWords(String sentence) {
int ans = 0;
for (String token : sentence.split(" ")) {
if (check(token)) {
++ans;
}
for (String s : sentence.split(" ")) {
ans += check(s.toCharArray());
}
return ans;
}

private boolean check(String token) {
int n = token.length();
if (n == 0) {
return false;
private int check(char[] s) {
if (s.length == 0) {
return 0;
}
boolean hyphen = false;
for (int i = 0; i < n; ++i) {
char c = token.charAt(i);
if (Character.isDigit(c) || (i < n - 1 && (c == '!' || c == '.' || c == ','))) {
return false;
boolean st = false;
for (int i = 0; i < s.length; ++i) {
if (Character.isDigit(s[i])) {
return 0;
}
if ((s[i] == '!' || s[i] == '.' || s[i] == ',') && i < s.length - 1) {
return 0;
}
if (c == '-') {
if (hyphen || i == 0 || i == n - 1 || !Character.isLetter(token.charAt(i - 1))
|| !Character.isLetter(token.charAt(i + 1))) {
return false;
if (s[i] == '-') {
if (st || i == 0 || i == s.length - 1) {
return 0;
}
hyphen = true;
if (!Character.isAlphabetic(s[i - 1]) || !Character.isAlphabetic(s[i + 1])) {
return 0;
}
st = true;
}
}
return true;
return 1;
}
}
```

#### TypeScript
#### C++

```ts
function countValidWords(sentence: string): number {
let words = sentence.trim().split(/\s+/);
let ans = 0;
for (let word of words) {
if (isValied(word)) {
ans++;
```cpp
class Solution {
public:
int countValidWords(string sentence) {
auto check = [](const string& s) -> int {
bool st = false;
for (int i = 0; i < s.length(); ++i) {
if (isdigit(s[i])) {
return 0;
}
if ((s[i] == '!' || s[i] == '.' || s[i] == ',') && i < s.length() - 1) {
return 0;
}
if (s[i] == '-') {
if (st || i == 0 || i == s.length() - 1) {
return 0;
}
if (!isalpha(s[i - 1]) || !isalpha(s[i + 1])) {
return 0;
}
st = true;
}
}
return 1;
};

int ans = 0;
stringstream ss(sentence);
string s;
while (ss >> s) {
ans += check(s);
}
return ans;
}
return ans;
};
```

#### Go

```go
func countValidWords(sentence string) (ans int) {
check := func(s string) int {
if len(s) == 0 {
return 0
}
st := false
for i, r := range s {
if unicode.IsDigit(r) {
return 0
}
if (r == '!' || r == '.' || r == ',') && i < len(s)-1 {
return 0
}
if r == '-' {
if st || i == 0 || i == len(s)-1 {
return 0
}
if !unicode.IsLetter(rune(s[i-1])) || !unicode.IsLetter(rune(s[i+1])) {
return 0
}
st = true
}
}
return 1
}
for _, s := range strings.Fields(sentence) {
ans += check(s)
}
return ans
}
```

#### TypeScript

function isValied(str: string): boolean {
let n = str.length;
let hasLine = false;
for (let i = 0; i < n; i++) {
const char = str.charAt(i);
if (/^[0-9]$/.test(char)) {
return false;
```ts
function countValidWords(sentence: string): number {
const check = (s: string): number => {
if (s.length === 0) {
return 0;
}
if (char == '-') {
if (hasLine) return false;
else {
hasLine = true;
let st = false;
for (let i = 0; i < s.length; ++i) {
if (/\d/.test(s[i])) {
return 0;
}
let pre = str.charAt(i - 1),
post = str.charAt(i + 1);
if (!/^[a-z]$/g.test(pre) || !/^[a-z]$/g.test(post)) {
return false;
if (['!', '.', ','].includes(s[i]) && i < s.length - 1) {
return 0;
}
if (s[i] === '-') {
if (st || [0, s.length - 1].includes(i)) {
return 0;
}
if (!/[a-zA-Z]/.test(s[i - 1]) || !/[a-zA-Z]/.test(s[i + 1])) {
return 0;
}
st = true;
}
}
if (/^[\!\.\,\s]$/.test(char) && i != n - 1) {
return false;
}
}
return true;
return 1;
};
return sentence.split(/\s+/).reduce((acc, s) => acc + check(s), 0);
}
```

Expand Down
Loading
Loading