Strong Root

난이도

 

문제를 보시려면 여기를 클릭

 

 

 

힌트

이거 해시 문제가 아니고 수학문제(경우의수)입니다 ;;;

해시인줄 알고 낚여서 한참 걸렸네요. 만약 고딩으로 돌아간다면 5분 안으로 풀었을 것 같아요.

팩토리얼, 콤비네이션, 퍼무테이션 이런거 쓰는거 아니고 순수 경우의 수 문제입니다.

 

 

 

이하는 코드입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.HashMap;
import java.util.Map;
 
public class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;
 
        Map<String, Integer> clothesMap = new HashMap<>();
 
        for (String[] cloth : clothes) {
            Integer count = clothesMap.get(cloth[1]);
 
            if (count == null) {
                clothesMap.put(cloth[1], 1);
            } else {
                clothesMap.put(cloth[1], count + 1);
            }
        }
 
        for (String cloth : clothesMap.keySet()) {
            int count = clothesMap.get(cloth);
            answer *= (count + 1);
        }
 
        answer--;
 
        return answer;
    }
}
cs

 

난이도

 

문제를 보시려면 여기를 클릭

 

 

 

힌트

저는 빅오를 최대한 낮춰보려고 모든 prefix 를 hash map 에 담아서 체크했습니다.

친구는 이렇게까지 안했는데도 통과되네요. 입력크기가 작아서 그런듯합니다.

 

 

 

이하는 코드입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.HashMap;
import java.util.Map;
 
public class Solution {
    public boolean solution(String[] phone_book) {
        Map<String, Integer> prefixMap = new HashMap<>();
 
        for (String phoneNo : phone_book) {
            for (int i = 1; i <= phoneNo.length(); i++) {
                String prefix = phoneNo.substring(0, i);
 
                Integer count = prefixMap.get(prefix);
 
                if (count == null) {
                    prefixMap.put(prefix, 1);
                } else {
                    prefixMap.put(prefix, count + 1);
                }
            }
        }
 
        for (String phoneNo : phone_book) {
            Integer count = prefixMap.get(phoneNo);
 
            if (count > 1) {
                return false;
            }
        }
 
        return true;
    }
}
cs

 

난이도 ★

 

문제를 보시려면 여기를 클릭

 

 

 

힌트

동명이인에 주의할 것.
그래서 Set 대신 Map 을 써야한다.

 

 

 

이하는 코드입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.HashMap;
import java.util.Map;
 
public class Solution {
    public String solution(String[] participant, String[] completion) {
        Map<String, Integer> completionSet = new HashMap<>();
 
        for (String name : completion) {
            Integer count = completionSet.get(name);
 
            if (count == null) {
                completionSet.put(name, 1);
            } else {
                completionSet.put(name, count + 1);
            }
        }
 
        for (String name : participant) {
            Integer count = completionSet.get(name);
 
            if (count == null) {
                return name;
            }
 
            if (count < 2) {
                completionSet.remove(name);
            } else {
                completionSet.put(name, count - 1);
            }
        }
 
        return null;
    }
}
cs

 


난이도 ★


문제를 보시려면 여기를 클릭




틀리기 쉬운 입출력 예제

입력

출력 

"abcdba"

"a"

1

1




힌트

 무려 n 세제곱의 풀서치를 하는 수밖에 없다. 대신 가지치기를 잘해야한다.

 1. 최대 길이를 구하는 것이므로 최대부터 서치할 것.

 2. 길이 1은 서치하지 말것.



이하는 코드입니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public int solution(String s) {
    for (int answer = s.length(); answer > 1; answer--) {
        int start = 0;
        int end = 0 + answer - 1;
        
        while (end < s.length()) {
            if (isPalindrome(s, start, end)) {
                return answer;
            }
            
            start++;
            end++;
        }
    }
    
    return 1;
}
 
private boolean isPalindrome(String s, int start, int end) {
    int diffBy2 = (end - start + 1/ 2 - 1;
    
    for (int i = 0; i <= diffBy2; i++) {
        char c1 = s.charAt(start + i);
        char c2 = s.charAt(end - i);
        
        if (c1 != c2) {
            return false;
        }
    }
    
    return true;
}
cs