Skip to content

Commit 7440cfa

Browse files
committed
feat: add solutions to lc problem: No.2351
No.2351.First Letter to Appear Twice
1 parent 27c01f6 commit 7440cfa

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

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

+84
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,91 @@ func repeatedCharacter(s string) byte {
183183
### **TypeScript**
184184

185185
```ts
186+
function repeatedCharacter(s: string): string {
187+
const vis = new Array(26).fill(false);
188+
for (const c of s) {
189+
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
190+
if (vis[i]) {
191+
return c;
192+
}
193+
vis[i] = true;
194+
}
195+
return ' ';
196+
}
197+
```
198+
199+
```ts
200+
function repeatedCharacter(s: string): string {
201+
let mask = 0;
202+
for (const c of s) {
203+
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
204+
if (mask & (1 << i)) {
205+
return c;
206+
}
207+
mask |= 1 << i;
208+
}
209+
return ' ';
210+
}
211+
```
212+
213+
### **Rust**
214+
215+
```rust
216+
impl Solution {
217+
pub fn repeated_character(s: String) -> char {
218+
let mut vis = [false; 26];
219+
for &c in s.as_bytes() {
220+
if vis[(c - b'a') as usize] {
221+
return c as char;
222+
}
223+
vis[(c - b'a') as usize] = true;
224+
}
225+
' '
226+
}
227+
}
228+
```
229+
230+
```rust
231+
impl Solution {
232+
pub fn repeated_character(s: String) -> char {
233+
let mut mask = 0;
234+
for &c in s.as_bytes() {
235+
if mask & 1 << (c - b'a') as i32 != 0 {
236+
return c as char;
237+
}
238+
mask |= 1 << (c - b'a') as i32;
239+
}
240+
' '
241+
}
242+
}
243+
```
244+
245+
### **C**
186246

247+
```c
248+
char repeatedCharacter(char *s) {
249+
int vis[26] = {0};
250+
for (int i = 0; s[i]; i++) {
251+
if (vis[s[i] - 'a']) {
252+
return s[i];
253+
}
254+
vis[s[i] - 'a']++;
255+
}
256+
return ' ';
257+
}
258+
```
259+
260+
```c
261+
char repeatedCharacter(char *s) {
262+
int mask = 0;
263+
for (int i = 0; s[i]; i++) {
264+
if (mask & (1 << s[i] - 'a')) {
265+
return s[i];
266+
}
267+
mask |= 1 << s[i] - 'a';
268+
}
269+
return ' ';
270+
}
187271
```
188272

189273
### **...**

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

+84
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,91 @@ func repeatedCharacter(s string) byte {
163163
### **TypeScript**
164164

165165
```ts
166+
function repeatedCharacter(s: string): string {
167+
const vis = new Array(26).fill(false);
168+
for (const c of s) {
169+
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
170+
if (vis[i]) {
171+
return c;
172+
}
173+
vis[i] = true;
174+
}
175+
return ' ';
176+
}
177+
```
178+
179+
```ts
180+
function repeatedCharacter(s: string): string {
181+
let mask = 0;
182+
for (const c of s) {
183+
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
184+
if (mask & (1 << i)) {
185+
return c;
186+
}
187+
mask |= 1 << i;
188+
}
189+
return ' ';
190+
}
191+
```
192+
193+
### **Rust**
194+
195+
```rust
196+
impl Solution {
197+
pub fn repeated_character(s: String) -> char {
198+
let mut vis = [false; 26];
199+
for &c in s.as_bytes() {
200+
if vis[(c - b'a') as usize] {
201+
return c as char;
202+
}
203+
vis[(c - b'a') as usize] = true;
204+
}
205+
' '
206+
}
207+
}
208+
```
209+
210+
```rust
211+
impl Solution {
212+
pub fn repeated_character(s: String) -> char {
213+
let mut mask = 0;
214+
for &c in s.as_bytes() {
215+
if mask & 1 << (c - b'a') as i32 != 0 {
216+
return c as char;
217+
}
218+
mask |= 1 << (c - b'a') as i32;
219+
}
220+
' '
221+
}
222+
}
223+
```
224+
225+
### **C**
166226

227+
```c
228+
char repeatedCharacter(char *s) {
229+
int vis[26] = {0};
230+
for (int i = 0; s[i]; i++) {
231+
if (vis[s[i] - 'a']) {
232+
return s[i];
233+
}
234+
vis[s[i] - 'a']++;
235+
}
236+
return ' ';
237+
}
238+
```
239+
240+
```c
241+
char repeatedCharacter(char *s) {
242+
int mask = 0;
243+
for (int i = 0; s[i]; i++) {
244+
if (mask & (1 << s[i] - 'a')) {
245+
return s[i];
246+
}
247+
mask |= 1 << s[i] - 'a';
248+
}
249+
return ' ';
250+
}
167251
```
168252

169253
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
char repeatedCharacter(char *s) {
2+
int mask = 0;
3+
for (int i = 0; s[i]; i++) {
4+
if (mask & (1 << s[i] - 'a')) {
5+
return s[i];
6+
}
7+
mask |= 1 << s[i] - 'a';
8+
}
9+
return ' ';
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn repeated_character(s: String) -> char {
3+
let mut mask = 0;
4+
for &c in s.as_bytes() {
5+
if mask & 1 << (c - b'a') as i32 != 0 {
6+
return c as char;
7+
}
8+
mask |= 1 << (c - b'a') as i32;
9+
}
10+
' '
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function repeatedCharacter(s: string): string {
2+
let mask = 0;
3+
for (const c of s) {
4+
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
5+
if (mask & (1 << i)) {
6+
return c;
7+
}
8+
mask |= 1 << i;
9+
}
10+
return ' ';
11+
}

0 commit comments

Comments
 (0)