FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

First unique character · Scarlet-Coder/Java-Programs@394ebb5 · GitHub

Commit 394ebb5

Browse files
committed
First unique character
1 parent 61d3432 commit 394ebb5

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

‎FirstUniqueCharacter.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
3+
* <p>
4+
* Examples:
5+
* <p>
6+
* s = "leetcode"
7+
* return 0.
8+
* <p>
9+
* s = "loveleetcode",
10+
* return 2.
11+
*/
12+
public class FirstUniqueCharacter {
13+
14+
public int firstUniqChar(String s) {
15+
char[] chars = s.toCharArray();
16+
for (int i = 0; i < chars.length; i++) {
17+
char word = chars[i];
18+
if (!isContainsWord(chars, word, i)) {
19+
return i;
20+
}
21+
}
22+
return -1;
23+
}
24+
25+
private boolean isContainsWord(char[] words, char word, int currPos) {
26+
for (int i = 0; i < words.length; i++) {
27+
if (currPos == i) {
28+
continue;
29+
}
30+
if (words[i] == word) {
31+
return true;
32+
}
33+
}
34+
return false;
35+
}
36+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL