Skip to content

Commit 4043fc7

Browse files
committed
feat: add solutions to lc problem: No.2351
No.2351.First Letter to Appear Twice
1 parent 0955e26 commit 4043fc7

File tree

6 files changed

+175
-51
lines changed

6 files changed

+175
-51
lines changed

solution/2300-2399/2351.First Letter to Appear Twice/README.md

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:数组或哈希表**
55+
56+
遍历字符串 $s$,用数组或哈希表 `cnt` 记录每个字母出现的次数,当某个字母出现两次时,返回该字母。
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字符集大小。本题中 $C = 26$。
59+
60+
**方法二:位运算**
61+
62+
我们也可以用一个整数 `mask` 记录每个字母是否出现过,其中 `mask` 的第 $i$ 位表示第 $i$ 个字母是否出现过。当某个字母出现两次时,返回该字母。
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
65+
5466
<!-- tabs:start -->
5567

5668
### **Python3**
@@ -61,10 +73,21 @@
6173
class Solution:
6274
def repeatedCharacter(self, s: str) -> str:
6375
cnt = Counter()
64-
for v in s:
65-
cnt[v] += 1
66-
if cnt[v] == 2:
67-
return v
76+
for c in s:
77+
cnt[c] += 1
78+
if cnt[c] == 2:
79+
return c
80+
```
81+
82+
```python
83+
class Solution:
84+
def repeatedCharacter(self, s: str) -> str:
85+
mask = 0
86+
for c in s:
87+
i = ord(c) - ord('a')
88+
if mask >> i & 1:
89+
return c
90+
mask |= 1 << i
6891
```
6992

7093
### **Java**
@@ -75,12 +98,27 @@ class Solution:
7598
class Solution {
7699
public char repeatedCharacter(String s) {
77100
int[] cnt = new int[26];
78-
for (char c : s.toCharArray()) {
101+
for (int i = 0; ; ++i) {
102+
char c = s.charAt(i);
79103
if (++cnt[c - 'a'] == 2) {
80104
return c;
81105
}
82106
}
83-
return '.';
107+
}
108+
}
109+
```
110+
111+
```java
112+
class Solution {
113+
public char repeatedCharacter(String s) {
114+
int mask = 0;
115+
for (int i = 0; ; ++i) {
116+
char c = s.charAt(i);
117+
if ((mask >> (c - 'a') & 1) == 1) {
118+
return c;
119+
}
120+
mask |= 1 << (c - 'a');
121+
}
84122
}
85123
}
86124
```
@@ -91,10 +129,27 @@ class Solution {
91129
class Solution {
92130
public:
93131
char repeatedCharacter(string s) {
94-
vector<int> cnt(26);
95-
for (char c : s)
96-
if (++cnt[c - 'a'] == 2) return c;
97-
return '.';
132+
int cnt[26]{};
133+
for (int i = 0; ; ++i) {
134+
if (++cnt[s[i] - 'a'] == 2) {
135+
return s[i];
136+
}
137+
}
138+
}
139+
};
140+
```
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
char repeatedCharacter(string s) {
146+
int mask = 0;
147+
for (int i = 0; ; ++i) {
148+
if (mask >> (s[i] - 'a') & 1) {
149+
return s[i];
150+
}
151+
mask |= 1 << (s[i] - 'a');
152+
}
98153
}
99154
};
100155
```
@@ -103,14 +158,25 @@ public:
103158

104159
```go
105160
func repeatedCharacter(s string) byte {
106-
cnt := make([]int, 26)
107-
for _, c := range s {
108-
cnt[c-'a']++
109-
if cnt[c-'a'] == 2 {
110-
return byte(c)
161+
cnt := [26]int{}
162+
for i := 0; ; i++ {
163+
cnt[s[i]-'a']++
164+
if cnt[s[i]-'a'] == 2 {
165+
return s[i]
166+
}
167+
}
168+
}
169+
```
170+
171+
```go
172+
func repeatedCharacter(s string) byte {
173+
mask := 0
174+
for i := 0; ; i++ {
175+
if mask>>(s[i]-'a')&1 == 1 {
176+
return s[i]
111177
}
178+
mask |= 1 << (s[i] - 'a')
112179
}
113-
return '.'
114180
}
115181
```
116182

solution/2300-2399/2351.First Letter to Appear Twice/README_EN.md

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,21 @@ The only letter that appears twice is &#39;d&#39; so we return &#39;d&#39;.
5555
class Solution:
5656
def repeatedCharacter(self, s: str) -> str:
5757
cnt = Counter()
58-
for v in s:
59-
cnt[v] += 1
60-
if cnt[v] == 2:
61-
return v
58+
for c in s:
59+
cnt[c] += 1
60+
if cnt[c] == 2:
61+
return c
62+
```
63+
64+
```python
65+
class Solution:
66+
def repeatedCharacter(self, s: str) -> str:
67+
mask = 0
68+
for c in s:
69+
i = ord(c) - ord('a')
70+
if mask >> i & 1:
71+
return c
72+
mask |= 1 << i
6273
```
6374

6475
### **Java**
@@ -67,12 +78,27 @@ class Solution:
6778
class Solution {
6879
public char repeatedCharacter(String s) {
6980
int[] cnt = new int[26];
70-
for (char c : s.toCharArray()) {
81+
for (int i = 0; ; ++i) {
82+
char c = s.charAt(i);
7183
if (++cnt[c - 'a'] == 2) {
7284
return c;
7385
}
7486
}
75-
return '.';
87+
}
88+
}
89+
```
90+
91+
```java
92+
class Solution {
93+
public char repeatedCharacter(String s) {
94+
int mask = 0;
95+
for (int i = 0; ; ++i) {
96+
char c = s.charAt(i);
97+
if ((mask >> (c - 'a') & 1) == 1) {
98+
return c;
99+
}
100+
mask |= 1 << (c - 'a');
101+
}
76102
}
77103
}
78104
```
@@ -83,10 +109,27 @@ class Solution {
83109
class Solution {
84110
public:
85111
char repeatedCharacter(string s) {
86-
vector<int> cnt(26);
87-
for (char c : s)
88-
if (++cnt[c - 'a'] == 2) return c;
89-
return '.';
112+
int cnt[26]{};
113+
for (int i = 0; ; ++i) {
114+
if (++cnt[s[i] - 'a'] == 2) {
115+
return s[i];
116+
}
117+
}
118+
}
119+
};
120+
```
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
char repeatedCharacter(string s) {
126+
int mask = 0;
127+
for (int i = 0; ; ++i) {
128+
if (mask >> (s[i] - 'a') & 1) {
129+
return s[i];
130+
}
131+
mask |= 1 << (s[i] - 'a');
132+
}
90133
}
91134
};
92135
```
@@ -95,14 +138,25 @@ public:
95138

96139
```go
97140
func repeatedCharacter(s string) byte {
98-
cnt := make([]int, 26)
99-
for _, c := range s {
100-
cnt[c-'a']++
101-
if cnt[c-'a'] == 2 {
102-
return byte(c)
141+
cnt := [26]int{}
142+
for i := 0; ; i++ {
143+
cnt[s[i]-'a']++
144+
if cnt[s[i]-'a'] == 2 {
145+
return s[i]
146+
}
147+
}
148+
}
149+
```
150+
151+
```go
152+
func repeatedCharacter(s string) byte {
153+
mask := 0
154+
for i := 0; ; i++ {
155+
if mask>>(s[i]-'a')&1 == 1 {
156+
return s[i]
103157
}
158+
mask |= 1 << (s[i] - 'a')
104159
}
105-
return '.'
106160
}
107161
```
108162

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
class Solution {
22
public:
33
char repeatedCharacter(string s) {
4-
vector<int> cnt(26);
5-
for (char c : s)
6-
if (++cnt[c - 'a'] == 2) return c;
7-
return '.';
4+
int mask = 0;
5+
for (int i = 0; ; ++i) {
6+
if (mask >> (s[i] - 'a') & 1) {
7+
return s[i];
8+
}
9+
mask |= 1 << (s[i] - 'a');
10+
}
811
}
912
};
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
func repeatedCharacter(s string) byte {
2-
cnt := make([]int, 26)
3-
for _, c := range s {
4-
cnt[c-'a']++
5-
if cnt[c-'a'] == 2 {
6-
return byte(c)
2+
mask := 0
3+
for i := 0; ; i++ {
4+
if mask>>(s[i]-'a')&1 == 1 {
5+
return s[i]
76
}
7+
mask |= 1 << (s[i] - 'a')
88
}
9-
return '.'
109
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution {
22
public char repeatedCharacter(String s) {
3-
int[] cnt = new int[26];
4-
for (char c : s.toCharArray()) {
5-
if (++cnt[c - 'a'] == 2) {
3+
int mask = 0;
4+
for (int i = 0; ; ++i) {
5+
char c = s.charAt(i);
6+
if ((mask >> (c - 'a') & 1) == 1) {
67
return c;
78
}
9+
mask |= 1 << (c - 'a');
810
}
9-
return '.';
1011
}
1112
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class Solution:
22
def repeatedCharacter(self, s: str) -> str:
3-
cnt = Counter()
4-
for v in s:
5-
cnt[v] += 1
6-
if cnt[v] == 2:
7-
return v
3+
mask = 0
4+
for c in s:
5+
i = ord(c) - ord('a')
6+
if mask >> i & 1:
7+
return c
8+
mask |= 1 << i

0 commit comments

Comments
 (0)