[LeetCode] #387. First Unique Character in a String

Newone Tsai
Mar 25, 2021

--

[Easy][Question]:

Given a string, find the first non-repeating character in it and return its index. If it doesn’t exist, return -1.

Examples:

s = "leetcode"
return 0.
s = "loveleetcode"
return 2.

Note: You may assume the string contains only lowercase English letters.

My Solution[C++]:

[Ideas]: 用unordermap紀錄每個字元次數,再重string開頭開始找次數為1的return,若都沒有則return-1。

--

--

Newone Tsai
Newone Tsai

Written by Newone Tsai

I took the one less traveled by, and that has made all the difference.

No responses yet