Skip to content

Commit 5892d9c

Browse files
committed
issue #21 387-Done💯
1 parent 70d2158 commit 5892d9c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcodeEasyLevel;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class _0387FirstUniqueCharacterinaString {
10+
11+
public static int firstUniqChar(String s) {
12+
//sol1. Runtime 262 ms Memory 42.6 MB
13+
/*
14+
if(s.length() == 0) return -1;
15+
16+
String[] cArray = s.split("");
17+
List<String> ansList = new ArrayList<>();
18+
List<String> removedList = new ArrayList<>();
19+
for (String c : cArray) {
20+
if (!ansList.contains(c) && !removedList.contains(c)) {
21+
ansList.add(c);
22+
} else {
23+
ansList.remove(c);
24+
removedList.add(c);
25+
}
26+
//System.out.println("c: "+c+", ansList:"+ansList.toString()+", removedList: "+removedList.toString());
27+
}
28+
if (ansList.size() == 0) {
29+
return -1;
30+
}
31+
return s.indexOf(ansList.get(0));
32+
*/
33+
//sol2. Runtime 5 ms Memory 39.3 MB
34+
if(s.length()==1)return 0;
35+
int [] alpha=new int[26]; // to count how many time find char
36+
for(char x : s.toCharArray()) {
37+
alpha[x-97]++;
38+
System.out.println(x+", x-97: "+(x-97)+", alpha: "+alpha[x-97]);
39+
}
40+
for(int i=0;i<s.length();i++) {
41+
if(alpha[s.charAt(i)-97]==1)return i;
42+
}
43+
return -1;
44+
}
45+
46+
public static void main(String[] args) {
47+
System.out.println(firstUniqChar("leetcode")+", expected: "+0);
48+
//System.out.println(firstUniqChar("loveleetcode")+", expected: "+2);
49+
//System.out.println(firstUniqChar("")+", expected: "+-1);
50+
//System.out.println(firstUniqChar("aadadaad")+", expected: "+-1);
51+
}
52+
}

0 commit comments

Comments
 (0)