내 첫번째 풀이는 무식 그자체 ..
substring이용해서 풀었는데 . 효율성 테스트에서 Fail !
class Solution {
public boolean solution(String[] phone_book) {
boolean answer = true;
for (int i = 0; i < phone_book.length; i++) {
for (int j = 0; j < phone_book.length; j++) {
if (i != j && phone_book[j].startsWith(phone_book[i])) {
return false;
}
}
}
return answer;
}
}
public boolean startsWith(String prefix) - perfix 가 문자열의 접두사인지 아닌지를 판별해주는 메소드를 활용하여 다시 풀었음
package OneDayOneCodingChallenge;
import org.junit.Assert;
import org.junit.Test;
/**
* @author seoyeon on 2020/09/14
* @project AlgorithmTest 전화번호
*/
public class Day10 {
public boolean solution(String[] phone_book) {
boolean answer = true;
for (int i = 0; i < phone_book.length; i++) {
for (int j = i; j < phone_book.length; j++) {
if (phone_book[i].substring(0, Math.min(phone_book[i].length(), phone_book[j].length())).equals(phone_book[j]
.substring(0, Math.min(phone_book[i].length(), phone_book[j].length())))) {
if (i != j) {
answer = false;
break;
}
}
}
}
return answer;
}
@Test
public void 정답() {
Assert.assertEquals(false, solution(new String[]{"119", "97674223", "1195524421"}));
Assert.assertEquals(true, solution(new String[]{"123", "456", "789"}));
Assert.assertEquals(false, solution(new String[]{"12", "123", "1235", "567", "88"}));
}
}