-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhwawei1.java
More file actions
37 lines (32 loc) · 868 Bytes
/
hwawei1.java
File metadata and controls
37 lines (32 loc) · 868 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
32
33
34
35
36
37
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
/*input: BACCAAHEFGHFF*/
public class hwawei1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
System.out.println(findLastOnlyChar(input));
}
public static String findLastOnlyChar(String input){
Map<String, Integer> char2Count = new HashMap<String, Integer>();
Stack<String> charStack = new Stack<String>();
for(int i=0; i<input.length(); i++){
String c = String.valueOf(input.charAt(i));
charStack.push(c);
if(!char2Count.containsKey(c)){
char2Count.put(c, 1);
}else{
char2Count.put(c, char2Count.get(c)+1);
}
}
while(!charStack.isEmpty()){
String elem = charStack.pop();
if(char2Count.get(elem) == 1){
return elem;
}
}
return "NULL";
}
}