We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2507397 commit 600bb08Copy full SHA for 600bb08
EASY/src/easy/FirstUniqueCharacterinaString.java
@@ -0,0 +1,32 @@
1
+package easy;
2
+
3
+import java.util.HashMap;
4
+import java.util.Map;
5
6
+public class FirstUniqueCharacterinaString {
7
8
+ public static int firstUniqChar(String s) {
9
+ Map<Character, Integer> countMap = new HashMap();
10
+ Map<Character, Integer> indexMap = new HashMap();
11
12
+ for(int i = 0; i < s.length(); i++){
13
+ if(countMap.containsKey(s.charAt(i))){
14
+ countMap.put(s.charAt(i), countMap.get(s.charAt(i))+1);
15
+ } else {
16
+ indexMap.put(s.charAt(i), i);
17
+ countMap.put(s.charAt(i), 1);
18
+ }
19
20
21
22
+ if(countMap.get(s.charAt(i)) == 1) return indexMap.get(s.charAt(i));
23
24
+ return -1;
25
26
27
+ public static void main(String...strings){
28
+ String s = "leetcode";
29
+ System.out.println(firstUniqChar(s));
30
31
32
+}
0 commit comments