Skip to content

Commit 10a5512

Browse files
committed
feat: update solutions to lc problem: No.0387
No.0387.First Unique Character in a String
1 parent ba46fd1 commit 10a5512

File tree

9 files changed

+127
-107
lines changed

9 files changed

+127
-107
lines changed

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

+52-40
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47-
遍历字符串,用一个 map 或者字典存放字符串中每个字符出现的次数。然后再次遍历字符串,取出对应字符出现的次数,若次数为 1,直接返回当前字符串的下标。遍历结束,返回 -1。
47+
**方法一:数组或哈希表**
48+
49+
我们可以用数组或哈希表 $cnt$ 记录字符串 $s$ 中每个字符出现的次数。
50+
51+
然后我们再遍历字符串 $s$,当遍历到某个字符 $c$ 时,如果 $cnt[c]=1$,则说明 $c$ 是第一个不重复的字符,返回它的索引即可。
52+
53+
如果遍历完字符串 $s$ 仍然没有找到不重复的字符,返回 $-1$。
54+
55+
时间复杂度 $O(n)$,空间复杂度 $O(\Sigma)$,其中 $\Sigma$ 是字符集的大小。
4856

4957
<!-- tabs:start -->
5058

@@ -55,9 +63,9 @@
5563
```python
5664
class Solution:
5765
def firstUniqChar(self, s: str) -> int:
58-
counter = Counter(s)
66+
cnt = Counter(s)
5967
for i, c in enumerate(s):
60-
if counter[c] == 1:
68+
if cnt[c] == 1:
6169
return i
6270
return -1
6371
```
@@ -69,13 +77,13 @@ class Solution:
6977
```java
7078
class Solution {
7179
public int firstUniqChar(String s) {
72-
int[] counter = new int[26];
73-
for (char c : s.toCharArray()) {
74-
++counter[c - 'a'];
80+
int[] cnt = new int[26];
81+
int n = s.length();
82+
for (int i = 0; i < n; ++i) {
83+
++cnt[s.charAt(i) - 'a'];
7584
}
76-
for (int i = 0; i < s.length(); ++i) {
77-
char c = s.charAt(i);
78-
if (counter[c - 'a'] == 1) {
85+
for (int i = 0; i < n; ++i) {
86+
if (cnt[s.charAt(i) - 'a'] == 1) {
7987
return i;
8088
}
8189
}
@@ -84,32 +92,22 @@ class Solution {
8492
}
8593
```
8694

87-
### **TypeScript**
88-
89-
```ts
90-
function firstUniqChar(s: string): number {
91-
let record = new Map();
92-
for (let cur of [...s]) {
93-
record.set(cur, record.has(cur));
94-
}
95-
for (let i = 0; i < s.length; i++) {
96-
if (!record.get(s[i])) return i;
97-
}
98-
return -1;
99-
}
100-
```
101-
10295
### **C++**
10396

10497
```cpp
10598
class Solution {
10699
public:
107100
int firstUniqChar(string s) {
108-
vector<int> counter(26);
109-
for (char& c : s) ++counter[c - 'a'];
110-
for (int i = 0; i < s.size(); ++i)
111-
if (counter[s[i] - 'a'] == 1)
101+
int cnt[26]{};
102+
for (char& c : s) {
103+
++cnt[c - 'a'];
104+
}
105+
int n = s.size();
106+
for (int i = 0; i < n; ++i) {
107+
if (cnt[s[i] - 'a'] == 1) {
112108
return i;
109+
}
110+
}
113111
return -1;
114112
}
115113
};
@@ -119,12 +117,12 @@ public:
119117
120118
```go
121119
func firstUniqChar(s string) int {
122-
counter := make([]int, 26)
120+
cnt := [26]int{}
123121
for _, c := range s {
124-
counter[c-'a']++
122+
cnt[c-'a']++
125123
}
126124
for i, c := range s {
127-
if counter[c-'a'] == 1 {
125+
if cnt[c-'a'] == 1 {
128126
return i
129127
}
130128
}
@@ -140,19 +138,36 @@ func firstUniqChar(s string) int {
140138
* @return {number}
141139
*/
142140
var firstUniqChar = function (s) {
143-
const counter = new Map();
144-
for (let c of s) {
145-
counter[c] = (counter[c] || 0) + 1;
141+
const cnt = new Array(26).fill(0);
142+
for (const c of s) {
143+
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
146144
}
147145
for (let i = 0; i < s.length; ++i) {
148-
if (counter[s[i]] == 1) {
146+
if (cnt[s[i].charCodeAt() - 'a'.charCodeAt()] === 1) {
149147
return i;
150148
}
151149
}
152150
return -1;
153151
};
154152
```
155153

154+
### **TypeScript**
155+
156+
```ts
157+
function firstUniqChar(s: string): number {
158+
const cnt = new Array(26).fill(0);
159+
for (const c of s) {
160+
cnt[c.charCodeAt(0) - 97]++;
161+
}
162+
for (let i = 0; i < s.length; i++) {
163+
if (cnt[s.charCodeAt(i) - 97] === 1) {
164+
return i;
165+
}
166+
}
167+
return -1;
168+
}
169+
```
170+
156171
### **PHP**
157172

158173
```php
@@ -162,14 +177,11 @@ class Solution {
162177
* @return Integer
163178
*/
164179
function firstUniqChar($s) {
165-
$hashmap = [];
166180
for ($i = 0; $i < strlen($s); $i++) {
167-
$word = $s[$i];
168-
$hasmap[$word] += 1;
181+
$hashtable[$s[$i]]++;
169182
}
170183
for ($i = 0; $i < strlen($s); $i++) {
171-
$word = $s[$i];
172-
if ($hasmap[$word] == 1) {
184+
if ($hashtable[$s[$i]] == 1) {
173185
return $i;
174186
}
175187
}

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

+43-39
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
```python
3535
class Solution:
3636
def firstUniqChar(self, s: str) -> int:
37-
counter = Counter(s)
37+
cnt = Counter(s)
3838
for i, c in enumerate(s):
39-
if counter[c] == 1:
39+
if cnt[c] == 1:
4040
return i
4141
return -1
4242
```
@@ -46,13 +46,13 @@ class Solution:
4646
```java
4747
class Solution {
4848
public int firstUniqChar(String s) {
49-
int[] counter = new int[26];
50-
for (char c : s.toCharArray()) {
51-
++counter[c - 'a'];
49+
int[] cnt = new int[26];
50+
int n = s.length();
51+
for (int i = 0; i < n; ++i) {
52+
++cnt[s.charAt(i) - 'a'];
5253
}
53-
for (int i = 0; i < s.length(); ++i) {
54-
char c = s.charAt(i);
55-
if (counter[c - 'a'] == 1) {
54+
for (int i = 0; i < n; ++i) {
55+
if (cnt[s.charAt(i) - 'a'] == 1) {
5656
return i;
5757
}
5858
}
@@ -61,32 +61,22 @@ class Solution {
6161
}
6262
```
6363

64-
### **TypeScript**
65-
66-
```ts
67-
function firstUniqChar(s: string): number {
68-
let record = new Map();
69-
for (let cur of [...s]) {
70-
record.set(cur, record.has(cur));
71-
}
72-
for (let i = 0; i < s.length; i++) {
73-
if (!record.get(s[i])) return i;
74-
}
75-
return -1;
76-
}
77-
```
78-
7964
### **C++**
8065

8166
```cpp
8267
class Solution {
8368
public:
8469
int firstUniqChar(string s) {
85-
vector<int> counter(26);
86-
for (char& c : s) ++counter[c - 'a'];
87-
for (int i = 0; i < s.size(); ++i)
88-
if (counter[s[i] - 'a'] == 1)
70+
int cnt[26]{};
71+
for (char& c : s) {
72+
++cnt[c - 'a'];
73+
}
74+
int n = s.size();
75+
for (int i = 0; i < n; ++i) {
76+
if (cnt[s[i] - 'a'] == 1) {
8977
return i;
78+
}
79+
}
9080
return -1;
9181
}
9282
};
@@ -96,12 +86,12 @@ public:
9686
9787
```go
9888
func firstUniqChar(s string) int {
99-
counter := make([]int, 26)
89+
cnt := [26]int{}
10090
for _, c := range s {
101-
counter[c-'a']++
91+
cnt[c-'a']++
10292
}
10393
for i, c := range s {
104-
if counter[c-'a'] == 1 {
94+
if cnt[c-'a'] == 1 {
10595
return i
10696
}
10797
}
@@ -117,19 +107,36 @@ func firstUniqChar(s string) int {
117107
* @return {number}
118108
*/
119109
var firstUniqChar = function (s) {
120-
const counter = new Map();
121-
for (let c of s) {
122-
counter[c] = (counter[c] || 0) + 1;
110+
const cnt = new Array(26).fill(0);
111+
for (const c of s) {
112+
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
123113
}
124114
for (let i = 0; i < s.length; ++i) {
125-
if (counter[s[i]] == 1) {
115+
if (cnt[s[i].charCodeAt() - 'a'.charCodeAt()] === 1) {
126116
return i;
127117
}
128118
}
129119
return -1;
130120
};
131121
```
132122

123+
### **TypeScript**
124+
125+
```ts
126+
function firstUniqChar(s: string): number {
127+
const cnt = new Array(26).fill(0);
128+
for (const c of s) {
129+
cnt[c.charCodeAt(0) - 97]++;
130+
}
131+
for (let i = 0; i < s.length; i++) {
132+
if (cnt[s.charCodeAt(i) - 97] === 1) {
133+
return i;
134+
}
135+
}
136+
return -1;
137+
}
138+
```
139+
133140
### **PHP**
134141

135142
```php
@@ -139,14 +146,11 @@ class Solution {
139146
* @return Integer
140147
*/
141148
function firstUniqChar($s) {
142-
$hashmap = [];
143149
for ($i = 0; $i < strlen($s); $i++) {
144-
$word = $s[$i];
145-
$hasmap[$word] += 1;
150+
$hashtable[$s[$i]]++;
146151
}
147152
for ($i = 0; $i < strlen($s); $i++) {
148-
$word = $s[$i];
149-
if ($hasmap[$word] == 1) {
153+
if ($hashtable[$s[$i]] == 1) {
150154
return $i;
151155
}
152156
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
class Solution {
22
public:
33
int firstUniqChar(string s) {
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)
4+
int cnt[26]{};
5+
for (char& c : s) {
6+
++cnt[c - 'a'];
7+
}
8+
int n = s.size();
9+
for (int i = 0; i < n; ++i) {
10+
if (cnt[s[i] - 'a'] == 1) {
811
return i;
12+
}
13+
}
914
return -1;
1015
}
1116
};

Diff for: solution/0300-0399/0387.First Unique Character in a String/Solution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
func firstUniqChar(s string) int {
2-
counter := make([]int, 26)
2+
cnt := [26]int{}
33
for _, c := range s {
4-
counter[c-'a']++
4+
cnt[c-'a']++
55
}
66
for i, c := range s {
7-
if counter[c-'a'] == 1 {
7+
if cnt[c-'a'] == 1 {
88
return i
99
}
1010
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public int firstUniqChar(String s) {
3-
int[] counter = new int[26];
4-
for (char c : s.toCharArray()) {
5-
++counter[c - 'a'];
3+
int[] cnt = new int[26];
4+
int n = s.length();
5+
for (int i = 0; i < n; ++i) {
6+
++cnt[s.charAt(i) - 'a'];
67
}
7-
for (int i = 0; i < s.length(); ++i) {
8-
char c = s.charAt(i);
9-
if (counter[c - 'a'] == 1) {
8+
for (int i = 0; i < n; ++i) {
9+
if (cnt[s.charAt(i) - 'a'] == 1) {
1010
return i;
1111
}
1212
}

Diff for: solution/0300-0399/0387.First Unique Character in a String/Solution.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* @return {number}
44
*/
55
var firstUniqChar = function (s) {
6-
const counter = new Map();
7-
for (let c of s) {
8-
counter[c] = (counter[c] || 0) + 1;
6+
const cnt = new Array(26).fill(0);
7+
for (const c of s) {
8+
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
99
}
1010
for (let i = 0; i < s.length; ++i) {
11-
if (counter[s[i]] == 1) {
11+
if (cnt[s[i].charCodeAt() - 'a'.charCodeAt()] === 1) {
1212
return i;
1313
}
1414
}

0 commit comments

Comments
 (0)