mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-03 05:27:36 +00:00
docs(docs): migrate to array based folder structure
This commit is contained in:
1 parent
a7f9fc28e0
commit
172bdcf40e
24 files changed
+64
-2914
No files matched your search
@@ -0,0 +1,50 @@
|
||||
---
|
||||
title: LC-387
|
||||
description: First Unique Character in a String
|
||||
---
|
||||
|
||||
## Examples
|
||||
|
||||
| Input | Output |
|
||||
| ---------------- | ------ |
|
||||
| `"leetcode"` | `0` |
|
||||
| `"loveleetcode"` | `2` |
|
||||
| `"aabb"` | `-1` |
|
||||
|
||||
## Hints
|
||||
|
||||
<Accordions type="single">
|
||||
<Accordion title="Constraints">
|
||||
Constraints:
|
||||
• `1 <= s.length <= 10^5`
|
||||
• `s` consists of only lowercase English letters.
|
||||
</Accordion>
|
||||
<Accordion title="Problem Statement">
|
||||
Given a string `s`, find the first non-repeating character in it and return its index.
|
||||
If it does not exist, return `-1`.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
## Solution
|
||||
|
||||
```js
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {number}
|
||||
*/
|
||||
|
||||
var firstUniqChar = function (s) {
|
||||
// Default for Hammer
|
||||
const freq = {};
|
||||
for (let c of s) freq[c] = (freq[c] || 0) + 1;
|
||||
|
||||
// Problem Specific
|
||||
// Loop the ORIGINAL string — we want an index, so no sorting
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
if (freq[s[i]] === 1) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
```
|
||||
Reference in new issue
Block a user