Skip to content

Commit 8f8108f

Browse files
committed
[LeetCode Sync] Runtime - 47 ms (88.88%), Memory - 18 MB (83.01%)
1 parent 56dbdf5 commit 8f8108f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<div class="example-block">
7+
<p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p>
8+
9+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
10+
11+
<p><strong>Explanation:</strong></p>
12+
13+
<p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p>
14+
</div>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<div class="example-block">
19+
<p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p>
20+
21+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
22+
</div>
23+
24+
<p><strong class="example">Example 3:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
30+
</div>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
37+
<li><code>s</code> consists of only lowercase English letters.</li>
38+
</ul>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def firstUniqChar(self, s: str) -> int:
3+
count = Counter(s)
4+
for i, char in enumerate(s):
5+
if count[char] == 1:
6+
return i
7+
return -1

0 commit comments

Comments
 (0)