-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFirstUniqueCharacter.java
75 lines (63 loc) · 1.8 KB
/
FirstUniqueCharacter.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*https://leetcode.com/problems/first-unique-character-in-a-string/*/
//hashtable solution
class Solution {
public int firstUniqChar(String s)
{
int[] hashTable = new int[26];
for (int i = 0; i < 26; ++i)
hashTable[i] = -1;
for (int i = 0; i < s.length(); ++i)
{
char ch = s.charAt(i);
//store the first index
if (hashTable[ch-'a'] == -1)
hashTable[ch-'a'] = i;
//if encountered for the second time, store -inf
else
hashTable[ch-'a'] = Integer.MIN_VALUE;
}
int pos=-1;
for (int i = 0; i < 26; ++i)
{
//if -1 or -inf, continue
if (hashTable[i] == Integer.MIN_VALUE || hashTable[i] == -1) continue;
//update pos
if (pos == -1 || pos > hashTable[i])
pos = hashTable[i];
}
return pos;
}
}
//efficient solution
class Solution {
public int firstUniqChar(String s) {
int index = s.length();
for (char i = 'a'; i <= 'z'; ++i) {
//get the first index
int first = s.indexOf(i);
//if the character is present and the last index is same as the first index
if (first != -1 && first == s.lastIndexOf(i)) {
//update index
if (index > first) {
index = first;
}
}
}
//return index
if (index != s.length()) {
return index;
}
return -1;
}
}
/*Pratik's Solution*/
class Solution {
public int firstUniqChar(String s)
{
for(int i=0;i<s.length();i++)
{
if(s.indexOf(s.charAt(i))==s.lastIndexOf(s.charAt(i)))return i;
}
return -1;
}
}