Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- greedy
- string
- 코테
- Binary Search
- 구현
- array
- dynamic programming
- Binary Tree
- Stack
- Number Theory
- Counting
- SQL
- Class
- Tree
- 코딩테스트
- sorting
- hash table
- two pointers
- 파이썬
- Method
- java
- database
- simulation
- Matrix
- implement
- Math
- bit manipulation
- Data Structure
- geometry
- 자바
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2047. Number of Valid Words in a Sentence 본문
1. Input
1) String sentence
- sentence는 1개 이상의 공백 문자로 구분되는 1개 이상의 토큰으로 쪼개질 수 있다.
2. Output
1) 다음 조건을 만족하는 토큰의 개수를 반환
- 숫자는 포함하지 않아야 한다.
- 하이픈(-)은 최대 1개까지 있을 수 있으며, 반드시 영어 소문자로 둘러싸여 있어야 한다.
- 문장부호는 최대 1개까지 있을 수 있으며, 만약 있다면 토큰의 맨 끝에 있어야 한다.
3. Constraint
1) 1 <= sentence.length <= 1000
2) sentence는 영어 소문자, 숫자, 공백 문자, 특수문자 -!,.로 이루어져 있다.
3) 토큰은 최소 1개 있다.
4. Example
Input: sentence = "alice and bob are playing stone-game10" -> Output: 5
설명: 총 6개의 토큰이 있다. 이중 유효하지 않은 토큰은 "stone-game10"이며, 이유는 숫자가 포함되어있기 때문이다.
5. Code
1) 첫 코드(2023/04/26)
import java.util.*;
class Solution {
public int countValidWords(String sentence) {
StringTokenizer token = new StringTokenizer(sentence);
int count = token.countTokens();
while(token.hasMoreTokens()){
String s = token.nextToken();
for(int i=0 ; i<s.length() ; i++){
char c = s.charAt(i);
if(Character.isDigit(c)){
count--; break;
}
if(c=='-' && !s.matches("[a-z]+-[a-z]+[//.!,]?")){
count--; break;
}
if((c=='!' || c=='.' || c==',') && i!=s.length()-1){
count--; break;
}
}
}
return count;
}
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2073. Time Needed to Buy Tickets (0) | 2023.04.26 |
---|---|
[LeetCode/Easy] 2053. Kth Distinct String in an Array (0) | 2023.04.26 |
[LeetCode/Easy] 2027. Minimum Moves to Convert String (0) | 2023.04.26 |
[프로그래머스/Lv.0] 문자열 여러 번 뒤집기 (0) | 2023.04.25 |
[프로그래머스/Lv.0] 배열 만들기 2 (0) | 2023.04.25 |