프로그래밍/알고리즘 문제풀이
codewars: Counting Duplicates
noveljava
2019. 7. 4. 16:02
문제
- https://www.codewars.com/kata/counting-duplicates
Codewars: Train your coding skills
Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.
www.codewars.com
문제풀이
- 입력된 문자열에서, 중복된 char가 몇개 있는지 체크하는 프로그램입니다.
- 그렇기에, a~z까지의 배열을 생성하고, 문자열을 순회하면서 해당 문자가 몇번 나왔는지 확인을 하게 됩니다.
- 그 이후에, 지금까지 나온 갯수를 판단하여 1개 이상이 나왔다면, resultCnt를 올려주어 return 시켜주게 됩니다.
public class CountingDuplicates {
public static int duplicateCount(String text) {
// Write your code here
int []ary = new int[26];
int resultCnt = 0;
for(int i=0; i<text.length(); ++i) {
char c = text.charAt(i);
if('a' <= c && c <= 'z') {
ary[c-'a']++;
}else if('A' <= c && c <= 'Z') {
ary[c-'A']++;
}
}
for(int i=0; i<26; ++i) {
if(ary[i] > 1) {
resultCnt++;
}
}
return resultCnt;
}
}