返回介绍

solution / 2000-2099 / 2047.Number of Valid Words in a Sentence / README_EN

发布于 2024-06-17 01:03:11 字数 5926 浏览 0 评论 0 收藏 0

2047. Number of Valid Words in a Sentence

中文文档

Description

A sentence consists of lowercase letters ('a' to 'z'), digits ('0' to '9'), hyphens ('-'), punctuation marks ('!', '.', and ','), and spaces (' ') only. Each sentence can be broken down into one or more tokens separated by one or more spaces ' '.

A token is a valid word if all three of the following are true:

  • It only contains lowercase letters, hyphens, and/or punctuation (no digits).
  • There is at most one hyphen '-'. If present, it must be surrounded by lowercase characters ("a-b" is valid, but "-ab" and "ab-" are not valid).
  • There is at most one punctuation mark. If present, it must be at the end of the token ("ab,", "cd!", and "." are valid, but "a!b" and "c.," are not valid).

Examples of valid words include "a-b.", "afad", "ba-c", "a!", and "!".

Given a string sentence, return _the number of valid words in _sentence.

 

Example 1:

Input: sentence = "cat and  dog"
Output: 3
Explanation: The valid words in the sentence are "cat", "and", and "dog".

Example 2:

Input: sentence = "!this  1-s b8d!"
Output: 0
Explanation: There are no valid words in the sentence.
"!this" is invalid because it starts with a punctuation mark.
"1-s" and "b8d" are invalid because they contain digits.

Example 3:

Input: sentence = "alice and  bob are playing stone-game10"
Output: 5
Explanation: The valid words in the sentence are "alice", "and", "bob", "are", and "playing".
"stone-game10" is invalid because it contains digits.

 

Constraints:

  • 1 <= sentence.length <= 1000
  • sentence only contains lowercase English letters, digits, ' ', '-', '!', '.', and ','.
  • There will be at least 1 token.

Solutions

Solution 1

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):
          return False
        if c == '-':
          if (
            hyphen
            or i == 0
            or i == len(token) - 1
            or not token[i - 1].islower()
            or not token[i + 1].islower()
          ):
            return False
          hyphen = True
      return True

    return sum(check(token) for token in sentence.split())
class Solution {
  public int countValidWords(String sentence) {
    int ans = 0;
    for (String token : sentence.split(" ")) {
      if (check(token)) {
        ++ans;
      }
    }
    return ans;
  }

  private boolean check(String token) {
    int n = token.length();
    if (n == 0) {
      return false;
    }
    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;
      }
      if (c == '-') {
        if (hyphen || i == 0 || i == n - 1 || !Character.isLetter(token.charAt(i - 1))
          || !Character.isLetter(token.charAt(i + 1))) {
          return false;
        }
        hyphen = true;
      }
    }
    return true;
  }
}
function countValidWords(sentence: string): number {
  let words = sentence.trim().split(/\s+/);
  let ans = 0;
  for (let word of words) {
    if (isValied(word)) {
      ans++;
    }
  }
  return ans;
}

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;
    }
    if (char == '-') {
      if (hasLine) return false;
      else {
        hasLine = true;
      }
      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 (/^[\!\.\,\s]$/.test(char) && i != n - 1) {
      return false;
    }
  }
  return true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文