Skip to content

Commit 7eb222f

Browse files
committed
feat: update solutions to lc problem: No.0387
No.0387.First Unique Character in a String
1 parent 5e377c2 commit 7eb222f

File tree

7 files changed

+173
-109
lines changed

7 files changed

+173
-109
lines changed

solution/0300-0399/0387.First Unique Character in a String/README.md

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,9 @@ s = "loveleetcode"
3939
```python
4040
class Solution:
4141
def firstUniqChar(self, s: str) -> int:
42-
chars = {}
43-
for ch in s:
44-
ch = ord(ch)
45-
chars[ch] = chars.get(ch, 0) + 1
46-
for i, ch in enumerate(s):
47-
ch = ord(ch)
48-
if chars[ch] == 1:
42+
counter = Counter(s)
43+
for i, c in enumerate(s):
44+
if counter[c] == 1:
4945
return i
5046
return -1
5147
```
@@ -57,15 +53,15 @@ class Solution:
5753
```java
5854
class Solution {
5955
public int firstUniqChar(String s) {
60-
Map<Character, Integer> chars = new HashMap<>(26);
61-
int n = s.length();
62-
for (int i = 0; i < n; ++i) {
63-
char ch = s.charAt(i);
64-
chars.put(ch, chars.getOrDefault(ch, 0) + 1);
56+
int[] counter = new int[26];
57+
for (char c : s.toCharArray()) {
58+
++counter[c - 'a'];
6559
}
66-
for (int i = 0; i < n; ++i) {
67-
char ch = s.charAt(i);
68-
if (chars.get(ch) == 1) return i;
60+
for (int i = 0; i < s.length(); ++i) {
61+
char c = s.charAt(i);
62+
if (counter[c - 'a'] == 1) {
63+
return i;
64+
}
6965
}
7066
return -1;
7167
}
@@ -87,6 +83,60 @@ function firstUniqChar(s: string): number {
8783
};
8884
```
8985

86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
int firstUniqChar(string s) {
92+
vector<int> counter(26);
93+
for (char& c : s) ++counter[c - 'a'];
94+
for (int i = 0; i < s.size(); ++i)
95+
if (counter[s[i] - 'a'] == 1)
96+
return i;
97+
return -1;
98+
}
99+
};
100+
```
101+
102+
### **Go**
103+
104+
```go
105+
func firstUniqChar(s string) int {
106+
counter := make([]int, 26)
107+
for _, c := range s {
108+
counter[c-'a']++
109+
}
110+
for i, c := range s {
111+
if counter[c-'a'] == 1 {
112+
return i
113+
}
114+
}
115+
return -1
116+
}
117+
```
118+
119+
### **JavaScript**
120+
121+
```js
122+
/**
123+
* @param {string} s
124+
* @return {number}
125+
*/
126+
var firstUniqChar = function(s) {
127+
const counter = new Map();
128+
for (let c of s) {
129+
counter[c] = (counter[c] || 0) + 1;
130+
}
131+
for (let i = 0; i < s.length; ++i) {
132+
if (counter[s[i]] == 1) {
133+
return i;
134+
}
135+
}
136+
return -1;
137+
};
138+
```
139+
90140
### **...**
91141

92142
```

solution/0300-0399/0387.First Unique Character in a String/README_EN.md

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,9 @@
3535
```python
3636
class Solution:
3737
def firstUniqChar(self, s: str) -> int:
38-
chars = {}
39-
for ch in s:
40-
ch = ord(ch)
41-
chars[ch] = chars.get(ch, 0) + 1
42-
for i, ch in enumerate(s):
43-
ch = ord(ch)
44-
if chars[ch] == 1:
38+
counter = Counter(s)
39+
for i, c in enumerate(s):
40+
if counter[c] == 1:
4541
return i
4642
return -1
4743
```
@@ -51,15 +47,15 @@ class Solution:
5147
```java
5248
class Solution {
5349
public int firstUniqChar(String s) {
54-
Map<Character, Integer> chars = new HashMap<>(26);
55-
int n = s.length();
56-
for (int i = 0; i < n; ++i) {
57-
char ch = s.charAt(i);
58-
chars.put(ch, chars.getOrDefault(ch, 0) + 1);
50+
int[] counter = new int[26];
51+
for (char c : s.toCharArray()) {
52+
++counter[c - 'a'];
5953
}
60-
for (int i = 0; i < n; ++i) {
61-
char ch = s.charAt(i);
62-
if (chars.get(ch) == 1) return i;
54+
for (int i = 0; i < s.length(); ++i) {
55+
char c = s.charAt(i);
56+
if (counter[c - 'a'] == 1) {
57+
return i;
58+
}
6359
}
6460
return -1;
6561
}
@@ -81,6 +77,60 @@ function firstUniqChar(s: string): number {
8177
};
8278
```
8379

80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
int firstUniqChar(string s) {
86+
vector<int> counter(26);
87+
for (char& c : s) ++counter[c - 'a'];
88+
for (int i = 0; i < s.size(); ++i)
89+
if (counter[s[i] - 'a'] == 1)
90+
return i;
91+
return -1;
92+
}
93+
};
94+
```
95+
96+
### **Go**
97+
98+
```go
99+
func firstUniqChar(s string) int {
100+
counter := make([]int, 26)
101+
for _, c := range s {
102+
counter[c-'a']++
103+
}
104+
for i, c := range s {
105+
if counter[c-'a'] == 1 {
106+
return i
107+
}
108+
}
109+
return -1
110+
}
111+
```
112+
113+
### **JavaScript**
114+
115+
```js
116+
/**
117+
* @param {string} s
118+
* @return {number}
119+
*/
120+
var firstUniqChar = function(s) {
121+
const counter = new Map();
122+
for (let c of s) {
123+
counter[c] = (counter[c] || 0) + 1;
124+
}
125+
for (let i = 0; i < s.length; ++i) {
126+
if (counter[s[i]] == 1) {
127+
return i;
128+
}
129+
}
130+
return -1;
131+
};
132+
```
133+
84134
### **...**
85135

86136
```
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
class Solution {
22
public:
33
int firstUniqChar(string s) {
4-
vector<int> cnts(26, 0), pos(26, s.size()) ;
5-
6-
7-
for (int i = s.size()-1; i >= 0; --i)
8-
{
9-
int index = s[i] - 'a' ;
10-
cnts[index]++ ;
11-
pos[index] = i ;
12-
}
13-
14-
int p = s.size() ;
15-
for (int i = 0; i < 26; ++i)
16-
{
17-
if (cnts[i] == 1 && pos[i] < p)
18-
p = pos[i] ;
19-
}
20-
21-
return p != s.size()? p: -1 ;
4+
vector<int> counter(26);
5+
for (char& c : s) ++counter[c - 'a'];
6+
for (int i = 0; i < s.size(); ++i)
7+
if (counter[s[i] - 'a'] == 1)
8+
return i;
9+
return -1;
2210
}
2311
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func firstUniqChar(s string) int {
2+
counter := make([]int, 26)
3+
for _, c := range s {
4+
counter[c-'a']++
5+
}
6+
for i, c := range s {
7+
if counter[c-'a'] == 1 {
8+
return i
9+
}
10+
}
11+
return -1
12+
}

solution/0300-0399/0387.First Unique Character in a String/Solution.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public int firstUniqChar(String s) {
3-
Map<Character, Integer> chars = new HashMap<>(26);
4-
int n = s.length();
5-
for (int i = 0; i < n; ++i) {
6-
char ch = s.charAt(i);
7-
chars.put(ch, chars.getOrDefault(ch, 0) + 1);
3+
int[] counter = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++counter[c - 'a'];
86
}
9-
for (int i = 0; i < n; ++i) {
10-
char ch = s.charAt(i);
11-
if (chars.get(ch) == 1) return i;
7+
for (int i = 0; i < s.length(); ++i) {
8+
char c = s.charAt(i);
9+
if (counter[c - 'a'] == 1) {
10+
return i;
11+
}
1212
}
1313
return -1;
1414
}
Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,16 @@
1-
const firstUniqChar2 = function (s) {
2-
let arr = {};
3-
for (let i = 0; i < s.length; i++) {
4-
// console.log(arr[s[i]]);
5-
if (arr[s[i]]) {
6-
arr[s[i]]++;
7-
} else {
8-
arr[s[i]] = 1;
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var firstUniqChar = function(s) {
6+
const counter = new Map();
7+
for (let c of s) {
8+
counter[c] = (counter[c] || 0) + 1;
99
}
10-
}
11-
let keys = Object.keys(arr);
12-
for (let i = 0; i < keys.length; i++) {
13-
if (arr[keys[i]] !== 1) {
14-
keys.splice(i, 1);
15-
i--;
10+
for (let i = 0; i < s.length; ++i) {
11+
if (counter[s[i]] == 1) {
12+
return i;
13+
}
1614
}
17-
}
18-
for (let i = 0; i < s.length; i++) {
19-
for (let j = 0; j < keys.length; j++) {
20-
if (s[i] === keys[j]) {
21-
return i;
22-
}
23-
}
24-
}
25-
return -1;
26-
};
27-
28-
const firstUniqChar = function (s) {
29-
let hashTable = {};
30-
let arr = [];
31-
32-
for (let i = 0; i < s.length; i++) {
33-
let c = s.charAt(i);
34-
if (!hashTable.hasOwnProperty(c)) {
35-
hashTable[c] = i;
36-
arr.push(i);
37-
} else {
38-
if (hashTable[c] !== null) {
39-
let val = hashTable[c];
40-
let index = arr.indexOf(val);
41-
arr.splice(index, 1);
42-
hashTable[c] = null;
43-
}
44-
}
45-
}
46-
47-
return arr.length ? arr[0] : -1;
48-
};
15+
return -1;
16+
};
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
class Solution:
22
def firstUniqChar(self, s: str) -> int:
3-
chars = {}
4-
for ch in s:
5-
ch = ord(ch)
6-
chars[ch] = chars.get(ch, 0) + 1
7-
for i, ch in enumerate(s):
8-
ch = ord(ch)
9-
if chars[ch] == 1:
3+
counter = Counter(s)
4+
for i, c in enumerate(s):
5+
if counter[c] == 1:
106
return i
117
return -1

0 commit comments

Comments
 (0)