forked from dnshi/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstUniqueCharacterInAString.js
More file actions
39 lines (35 loc) · 938 Bytes
/
FirstUniqueCharacterInAString.js
File metadata and controls
39 lines (35 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Source : https://leetcode.com/problems/first-unique-character-in-a-string
// Author : Dean Shi
// Date : 2017-03-08
/***************************************************************************************
*
* Given a string, find the first non-repeating character in it and return it's index.
* If it doesn't exist, return -1.
*
* Examples:
*
* s = "leetcode"
* return 0.
*
* s = "loveleetcode",
* return 2.
*
* Note: You may assume the string contain only lowercase letters.
*
*
***************************************************************************************/
/**
* @param {string} s
* @return {number}
*/
var firstUniqChar = function(s) {
const map = new Map()
for (let i = 0; i < s.length; i++) {
if (map.has(s[i])) map.set(s[i], false)
else map.set(s[i], i)
}
for (let [ch, index] of map) {
if (typeof index === 'number') return index
}
return -1
};