leecode/backlog/387.first-unique-character-in-a-string.js
2020-05-22 18:17:19 +08:00

44 lines
822 B
JavaScript

/*
* @lc app=leetcode id=387 lang=javascript
*
* [387] First Unique Character in a String
*
* https://leetcode.com/problems/first-unique-character-in-a-string/description/
*
* algorithms
* Easy (49.29%)
* Total Accepted: 255.6K
* Total Submissions: 513.8K
* Testcase Example: '"leetcode"'
*
*
* 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) {
for (let i = 0; i < s.length; i++) {
if (s.indexOf(s[i]) === s.lastIndexOf(s[i])) {
return i;
}
}
return -1;
};