-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion1.java
More file actions
31 lines (25 loc) · 937 Bytes
/
Copy pathQuestion1.java
File metadata and controls
31 lines (25 loc) · 937 Bytes
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
package practice2_hashing;
import java.util.HashMap;
public class Question1 {
public int solution(String s){
int answer = -1;
HashMap<Character, Integer> hs = new HashMap<>();
for (char x : s.toCharArray()) {
hs.put(x, hs.getOrDefault(x, 0) + 1);
}
for (int i = 0; i < s.length(); i++) {
if(hs.get(s.charAt(i)) == 1) { // 첫 번째로 등장하는 문자를 찾았다면
answer = i + 1; // answer를 현재 인덱스로 설정하고,
break; // 루프를 멈춥니다.
}
}
return answer;
}
public static void main(String[] args){
Question1 T = new Question1();
System.out.println(T.solution("statitsics"));
System.out.println(T.solution("aabb"));
System.out.println(T.solution("stringshowtime"));
System.out.println(T.solution("abcdeabcdfg"));
}
}