File tree 5 files changed +201
-0
lines changed
solution/2300-2399/2351.First Letter to Appear Twice
5 files changed +201
-0
lines changed Original file line number Diff line number Diff line change @@ -183,7 +183,91 @@ func repeatedCharacter(s string) byte {
183
183
### ** TypeScript**
184
184
185
185
``` 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**
186
246
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
+ }
187
271
```
188
272
189
273
### ** ...**
Original file line number Diff line number Diff line change @@ -163,7 +163,91 @@ func repeatedCharacter(s string) byte {
163
163
### ** TypeScript**
164
164
165
165
``` 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**
166
226
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
+ }
167
251
```
168
252
169
253
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments