코딩테스트 풀이/JAVA
[LeetCode/Easy] 1684. Count the Number of Consistent Strings
무지맘
2023. 1. 2. 13:13
1. Input
1) 문자열 allowed
2) 문자열 배열 words
2. Output
1) words의 요소 중 allowed에 있는 문자로만 이루어진 요소의 개수를 반환
3. Constraint
1) 1 <= words.length <= 104
2) 1 <= allowed.length <= 26
3) 1 <= words[i].length <= 10
4) allowed에 있는 문자들 중에 중복은 없다.
5) words와 allowed는 영어 소문자로만 이루어져 있다.
4. Example
Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"] -> Output: 2
설명: a나 b로만 이루어진 문자열은 “aaab”와 “baa” 두 가지이므로 2를 반환한다.
5. Code
1) 첫 코드(2022/06/12)
String pattern = "[" + allowed + "]+";
int count = 0;
for(int i=0 ; i<words.length ; i++){
if(words[i].matches(pattern))
count++;
}
return count;