Skip to content

Commit ea79b88

Browse files
committed
solve problem First Unique Character In A String
1 parent afce607 commit ea79b88

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ All solutions will be accepted!
140140
|205|[Isomorphic Strings](https://leetcode-cn.com/problems/isomorphic-strings/description/)|[java/py/js](./algorithms/IsomorphicStrings)|Easy|
141141
|125|[Valid Palindrome](https://leetcode-cn.com/problems/valid-palindrome/description/)|[java/py/js](./algorithms/ValidPalindrome)|Easy|
142142
|141|[Linked List Cycle](https://leetcode-cn.com/problems/linked-list-cycle/description/)|[java/py/js](./algorithms/LinkedListCycle)|Easy|
143+
|387|[First Unique Character In A String](https://leetcode-cn.com/problems/first-unique-character-in-a-string/description/)|[java/py/js](./algorithms/FirstUniqueCharacterInAString)|Easy|
143144

144145
# Database
145146
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# First Unique Character In A String
2+
This problme is easy to solve by hashmap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int firstUniqChar(String s) {
3+
Map<Character, Integer> characterMap = new HashMap<Character, Integer>();
4+
5+
for (int i = 0; i < s.length(); i++) {
6+
char c = s.charAt(i);
7+
if (characterMap.get(c) == null) {
8+
characterMap.put(c, 1);
9+
} else {
10+
characterMap.put(c, characterMap.get(c) + 1);
11+
}
12+
}
13+
14+
for (int i = 0; i < s.length(); i++) {
15+
if (characterMap.get(s.charAt(i)) == 1) {
16+
return i;
17+
}
18+
}
19+
return -1;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var firstUniqChar = function(s) {
6+
let characterMap = {}
7+
8+
for (let i = 0; i < s.length; i++) {
9+
let c = s[i]
10+
if (characterMap[c] === undefined) {
11+
characterMap[c] = 1
12+
} else {
13+
characterMap[c]++
14+
}
15+
}
16+
17+
for (let i = 0; i < s.length; i++) {
18+
if (characterMap[s[i]] === 1) {
19+
return i
20+
}
21+
}
22+
23+
return -1
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def firstUniqChar(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
character_map = {}
8+
9+
for c in s:
10+
if character_map.get(c) == None:
11+
character_map[c] = 1
12+
else:
13+
character_map[c] += 1
14+
15+
for i in range(len(s)):
16+
if character_map[s[i]] == 1:
17+
return i
18+
19+
return -1

0 commit comments

Comments
 (0)