209. first unique character in a string

explanation

  1. char : 256个
  2. return 可以是Character.MIN_VALUE
  3. array 当做map用

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class  {

* @param str: str: the given string
* @return: char: the first unique character in a given string
*/
public char firstUniqChar(String str) {
// Write your code here
int[] freq = new int[256];
for (int i = 0; i < str.length(); i++) {
freq[str.charAt(i)]++;
}
for (int i = 0; i < str.length(); i++) {
if (freq[str.charAt(i)] == 1) {
return str.charAt(i);
}
}
return Character.MIN_VALUE;

}
}

complexity

  1. space : O(1)
  2. time: O(n)