참고
문제해설
문제 로직고민
NOTE
•
동일 단어임을 판단하기 위해, 고려해야할 요소는 다음과 같다.
1.
단어의 길이가 같은가?
2.
길이가 같다면 사용된 알파벳의 종류와 개수가 같은가?
◦
1번의 경우는 쉽게 판별이 가능하고 2번의 경우는 알파벳의 개수를 체크하는 배열을 만들어서 확인해본다.
◦
단어는 100개 이하이며, 단어의 길이가 10이하 이므로, 100 * 10 * 26을 고려했을때 충분한 시간이다.
•
비슷한 단어의 조건은 다음과 같다.
◦
한 단어에서 하나의 문자를 더하거나, 빼거나, 하나의 문자를 다른 문자로 바꿀 경우 동일하게 변하는 경우
1.
단어의 길이차이 1, 사용된 알파벳 종류와 개수의 차이가 1인경우 (문자 추가 또는 제거)
2.
단어의 길이가 같고, 사용된 알파벳 종류와 개수의 차이가 2인경우 (다른 문자 변경)
작성코드
NOTE
public class _2607 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String[] s;
int N = Integer.parseInt(br.readLine());
int result = 0;
String firstWord = "";
String word;
int[] firstAlphabet = new int[26];
int[] alphabet = new int[26];
for(int i = 0; i<N; i++){
if(i==0){
firstWord = br.readLine();
for(char c : firstWord.toCharArray()){
++firstAlphabet[c - 'A'];
}
}
else{
word = br.readLine();
if(Math.abs(word.length() - firstWord.length()) < 2){
alphabet = new int[26];
int dif = 0;
for(char c : word.toCharArray()){
++alphabet[c-'A'];
}
for(int j =0; j<26; j++){
dif += Math.abs(firstAlphabet[j] - alphabet[j]);
}
if(dif < 2) result++;
else if(word.length() == firstWord.length() && dif == 2) result++;
}
}
}
System.out.println(result);
}
}
Java
복사
다른사람 풀이
NOTE
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int []arr = new int[26];
int []temp = new int[26];
int count=0;
String target = br.readLine();
for(int j=0;j<target.length();j++) {
arr[target.charAt(j)-'A'] +=1;
}
for(int i=0;i<n-1;i++) {
int cnt = 0;
temp=arr.clone();
String curr = br.readLine();
if(Math.abs(curr.length()-target.length())>1) continue;
for(int x=0;x<curr.length();x++) {
int idx = curr.charAt(x)-'A';
if(temp[idx]>0) {
temp[idx]--;
cnt++;
}
}
if(curr.length()-1 == target.length()) {
if(cnt==target.length()) count++;
}
else if(curr.length() == target.length()) {
if(cnt==target.length()-1||cnt==target.length()) count++;
}
else if(curr.length()+1 == target.length()) {
if(cnt==curr.length()) count++;
}
}
System.out.println(count);
}
}
Java
복사
유사한 풀이 코