首页 > 其他分享 >leetcode-2047-easy

leetcode-2047-easy

时间:2022-11-12 13:45:43浏览次数:34  
标签:count 2047 word easy sentence Pattern valid words leetcode

Number of Valid Words in a Sentence

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.

思路一:刚开始想自己写判断,但是分类的结果太多,所以也不考虑什么效率了,直接上正则

private static final Pattern pattern = Pattern.compile("^[a-z]+-[a-z]+([.!,])?$");
private static final Pattern pattern3 = Pattern.compile("^[a-z]+([.!,])?$");
private static final Pattern pattern2 = Pattern.compile("^[.!,]$");
public static int countValidWords(String sentence) {
	String[] words = sentence.split("\\s");
	int count = 0;
	for (String word : words) {
		if (pattern.matcher(word).find()) count++;
		if (pattern2.matcher(word).find()) count++;
		if (pattern3.matcher(word).find()) count++;
	}

	return count;
}

标签:count,2047,word,easy,sentence,Pattern,valid,words,leetcode
From: https://www.cnblogs.com/iyiluo/p/16883577.html

相关文章

  • LeetCode Hot1--两数之和
    两数之和给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输......
  • [回溯算法]leetcode216. 组合总和 III(c实现)
    题目找出所有相加之和为 n的 k 个数的组合,且满足下列条件:只使用数字1到9每个数字 最多使用一次 返回所有可能的有效组合的列表。该列表不能包含相同的组合两次......
  • 最大公约数 C/C++ leetcode , 辗转相除,更相减损
    #include <iostream>using namespace std;// 辗转相除法求最大公约数,用大的模小的,然后用除数模余数,该接口在新版的C++17的numeric 包中也有int gcd1(int a ,......
  • LeetCode 406.根据身高重建队列
    首先根据身高从小到大排序如果身高相等那么根据第二个值降序排序classSolution{public:vector<vector<int>>reconstructQueue(vector<vector<int>>&people){......
  • LeetCode刷题记录.Day12
    三数之和题目链接15.三数之和-力扣(LeetCode)classSolution{public:vector<vector<int>>threeSum(vector<int>&nums){vector<vector<int>>result......
  • 【leetcode_C++_二叉树_day12】层序遍历 10 && 226.翻转二叉树&&101. 对称二叉树
    1.层序遍历学会二叉树的层序遍历,可以一口气打完以下十题:102.二叉树的层序遍历107.二叉树的层次遍历II199.二叉树的右视图637.二叉树的层平均值429.N叉树的层序遍......
  • [oeasy]python0013_ASCII码表_英文字符编码_键盘字符
    ASCII码表回忆上次内容​ord(c)​​和​​chr(i)​这是俩函数这俩函数是一对,相反相成的⚖️​​ord​​通过​​字符​​找到对应的​​数字​​​​chr​​通过​​......
  • leetcode441
    排列硬币Category Difficulty Likes Dislikesalgorithms Easy(45.89%) 248 -TagsCompanies你总共有n枚硬币,并计划将它们按阶梯状排列。对于一个由k行组成的阶梯,......
  • Leetcode第1704题:判断字符串的两半是否相似(Determine is string halves are alike)
    解题思路直接模拟。将字符串分为两半,分别遍历统计各元音出现的次数,最后比较是否相等即可。核心代码如下:boolhalvesAreAlike(strings){stringa=s.substr(......
  • leetcode(35)位运算系列题目
    不需要额外空间的方法,就往位运算上想136.只出现一次的数字异或运算的性质:1.交换律:a^b^c<=>a^c^b2.任何数于0异或为任何数0^n=>n3.相同的数异或为0:......