===================== 해답 2가지 ===========================
이중 for문 사용할때는 결국엔 i = j 의 경우가 발생한다.
이 경우 생각한 값이 달라질 수 있는 변수가 되므로 반드시 조건문으로 어떻게 처리할 것인지 적어주는게 중요하다.
해답 1번.
package com.codestates.coplit;
import java.util.*;
public class Solution {
public boolean isIsogram(String str) {
/*
str 입력받아 아이소그램인지 여부 리턴.
<주의사항>
빈문자열 = true
대소문자 구분 x
<접근>
1. 모두 대문자로 변경
2. str.charAt(i)와 str.charAt(j)가 같은게 있는지 확인
*/
boolean answer = true;
String iso = str.toUpperCase();
if (iso.length() == 0) {
answer = true;
return answer;
}
for (int i = 0; i < iso.length(); i++) {
for (int j = 1; j < iso.length(); j++) {
if ( i == j ) { 여기
continue;
}
if (iso.charAt(i) == iso.charAt(j)) {
answer = false;
return answer;
}
}
}
return answer;
}
}
해답 2번.
if (iso.length() == 0) {
answer = true;
return answer;
}
for (int i = 0; i < iso.length(); i++) {
for (int j = 1; j < iso.length(); j++) {
if ( i != j ) { 여기
if (iso.charAt(i) == iso.charAt(j)) {
answer = false;
return answer;
}
}
}
}
return answer;
'알고리즘 문제 풀이' 카테고리의 다른 글
Q15. modulo // 성공 // int 타입 or long 타입 (0) | 2022.12.08 |
---|---|
Q14. superIncreasing // 1시간 안에 성공! (0) | 2022.12.07 |
Q10. 연속된 홀수 사이 "-" 추가하기. // update 완료 (0) | 2022.12.01 |
Q9. ABcheck // for문에서 중복되는 조건이 있을 경우 주의사항 ** (0) | 2022.11.29 |
<알고리즘 Q3 - 구현> 보드 게임 // equals() vs == (0) | 2022.11.28 |