

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
List<String> dictionary = new ArrayList<>();
for(int i = 0; i<n; i++){
dictionary.add(br.readLine());
}
int q = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for(int i = 0; i<q; i++){
String word = br.readLine();
...
}
// 단어가 사전에 있으면 correct
if (dictionary.contains(word)){
sb.append(word).append(" is correct\n");
}
else { // 아니면 비슷한 지 확인
// 사전을 돌면서
boolean isSimilar = false;
for (String dict_word : dictionary){
isSimilar = similarCheck(word, dict_word);
...
}
}
if (word_len == dword_len){
int count = 0;
for(int i = 0; i<word_len; i++){
if (word.charAt(i) != dict_word.charAt(i)){
count++;
}
}
...
}
if(count<2){ // 하나를 잘못 적은 경우
return true;
}
else if(count == 2){ // 두개가 다른 경우 (인접한)순서를 바꾸면 같은 지 확인
for(int i = 0; i<word_len-1; i++){
if (word.charAt(i) != dict_word.charAt(i)){
// 순서 바꿔보기
if(word.charAt(i) == dict_word.charAt(i+1) &&
word.charAt(i+1) == dict_word.charAt(i)){
return true;
}
}
}
}
else if(word_len == dword_len + 1){
if (word.contains(dict_word))
return true;
for(int i = 0; i<dword_len; i++){
//그 글자를 빼면 같아지는지 검증
if(word.charAt(i) != dict_word.charAt(i)){
if((word.substring(0, i) + word.substring(i + 2))
.equals(dict_word)){
return true;
}
}
}
}
// 하나를 적게 썼을 때
else if(word_len + 1 == dword_len){
if (dict_word.contains(word))
return true;
for(int i = 0; i<word_len; i++){
//사전단어에서 그 위치의 글자를 빼면 같아지는지 검증
if(word.charAt(i) != dict_word.charAt(i)){
if((dict_word.substring(0, i) + dict_word.substring(i + 1))
.equals(word)){
return true;
}
}
}
}
return false;
// 메인 함수에 있는 코드
if (isSimilar){
sb.append(word).append(" is misspelling of " + dict_word + "\n");
break;
}
}
if(!isSimilar) sb.append(word).append(" is unknown\n");
}
}
System.out.println(sb);
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
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());
List<String> dictionary = new ArrayList<>();
for(int i = 0; i<n; i++){
dictionary.add(br.readLine());
}
int q = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for(int i = 0; i<q; i++){
String word = br.readLine();
// 단어가 사전에 있으면 correct
if (dictionary.contains(word)){
sb.append(word).append(" is correct\n");
} else { // 아니면 비슷한 지 확인
// 사전을 돌면서
boolean isSimilar = false;
for (String dict_word : dictionary){
isSimilar = similarCheck(word, dict_word);
if (isSimilar){
sb.append(word).append(" is misspelling of " + dict_word + "\n");
break;
}
}
if(!isSimilar) sb.append(word).append(" is unknown\n");
}
}
System.out.println(sb);
}
public static boolean similarCheck(String word, String dict_word){
int word_len = word.length();
int dword_len = dict_word.length();
if (Math.abs(word_len - dword_len) > 2 )
return false;
if (word_len == dword_len){
int count = 0;
for(int i = 0; i<word_len; i++){
if (word.charAt(i) != dict_word.charAt(i)){
count++;
}
}
if(count<2){ // 하나를 잘못 적은 경우
return true;
}else if(count == 2){ // 두개가 다른 경우 (인접한)순서를 바꾸면 같은 지 확인
for(int i = 0; i<word_len-1; i++){
if (word.charAt(i) != dict_word.charAt(i)){
if(word.charAt(i) == dict_word.charAt(i+1) &&
word.charAt(i+1) == dict_word.charAt(i)){
return true;
}
}
}
}
} //하나를 많이 썼을 때
else if(word_len == dword_len + 1){
if (word.contains(dict_word))
return true;
for(int i = 0; i<dword_len; i++){
//그 글자를 빼면 같아지는지 검증
if(word.charAt(i) != dict_word.charAt(i)){
if((word.substring(0, i) + word.substring(i + 2))
.equals(dict_word)){
return true;
}
}
}
}
// 하나를 적게 썼을 때
else if(word_len + 1 == dword_len){
if (dict_word.contains(word))
return true;
for(int i = 0; i<word_len; i++){
//사전단어에서 그 위치의 글자를 빼면 같아지는지 검증
if(word.charAt(i) != dict_word.charAt(i)){
if((dict_word.substring(0, i) + dict_word.substring(i + 1))
.equals(word)){
return true;
}
}
}
}
return false;
}
}

정말 날 화나게 한 문제
언젠가 풀 수 있겠지?