Skip to content

Commit 2b0b658

Browse files
refactor 387
1 parent 349e320 commit 2b0b658

File tree

1 file changed

+19
-28
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+19
-28
lines changed
Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* 387. First Unique Character in a String
5-
* Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
6-
7-
Examples:
8-
9-
s = "leetcode"
10-
return 0.
11-
12-
s = "loveleetcode",
13-
return 2.
14-
Note: You may assume the string contain only lowercase letters.
15-
4+
* 387. First Unique Character in a String Given a string, find the first non-repeating character in
5+
* it and return it's index. If it doesn't exist, return -1.
6+
*
7+
* Examples:
8+
*
9+
* s = "leetcode" return 0.
10+
*
11+
* s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase letters.
1612
*/
1713

1814
public class _387 {
19-
15+
public static class Solution1 {
2016
public static int firstUniqChar(String s) {
21-
int[] freq = new int[26];
22-
for (int i = 0; i < s.length(); i++) {
23-
freq[s.charAt(i) - 'a']++;
17+
int[] freq = new int[26];
18+
for (int i = 0; i < s.length(); i++) {
19+
freq[s.charAt(i) - 'a']++;
20+
}
21+
for (int i = 0; i < s.length(); i++) {
22+
if (freq[s.charAt(i) - 'a'] == 1) {
23+
return i;
2424
}
25-
for (int i = 0; i < s.length(); i++) {
26-
if (freq[s.charAt(i) - 'a'] == 1) {
27-
return i;
28-
}
29-
}
30-
return -1;
31-
}
32-
33-
public static void main(String... strings) {
34-
String s = "leetcode";
35-
System.out.println(firstUniqChar(s));
25+
}
26+
return -1;
3627
}
37-
28+
}
3829
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy