Skip to content

Commit eca6d96

Browse files
committed
Add a solution to First Unique Character in a String
1 parent 53b1b1d commit eca6d96

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* [Microsoft](#microsoft)
2828

2929
## Progress
30-
[Problem Status](#problem-status) shows the latest progress to all 500+ questions. Currently we have 218 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
30+
[Problem Status](#problem-status) shows the latest progress to all 500+ questions. Currently we have 219 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3131

3232

3333
## Array
@@ -73,6 +73,7 @@
7373
| Title | Solution | Difficulty | Time | Space |
7474
| ----- | -------- | ---------- | ---- | ----- |
7575
[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)| [Swift](./FizzBuzz.swift)| Easy| O(n)| O(1)|
76+
[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)| [Swift](./FirstUniqueCharacterInString.swift)| Easy| O(n)| O(1)|
7677
[Keyboard Row](https://leetcode.com/problems/keyboard-row/)| [Swift](./KeyboardRow.swift)| Easy| O(nm)| O(n)|
7778
[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)| [Swift](./String/ValidPalindrome.swift)| Easy| O(n)| O(n)|
7879
[Detect Capital](https://leetcode.com/problems/detect-capital/)| [Swift](./String/DetectCapital.swift)| Easy| O(n)| O(1)|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/first-unique-character-in-a-string/
3+
* Primary idea: Keep track of existence of each character in the string
4+
*
5+
* Note: The maximum space of the dictionary is 26, so space complexity is O(1)
6+
*
7+
* Time Complexity: O(n), Space Complexity: O(1)
8+
*
9+
*/
10+
11+
class FirstUniqueCharacterInString {
12+
func firstUniqChar(_ s: String) -> Int {
13+
var dict = [Character: Bool]()
14+
15+
for char in s.characters {
16+
if let isDup = dict[char] {
17+
dict[char] = true
18+
} else {
19+
dict[char] = false
20+
}
21+
}
22+
23+
for (i, char) in Array(s.characters).enumerated() {
24+
if !dict[char]! {
25+
return i
26+
}
27+
}
28+
29+
return -1
30+
}
31+
}

0 commit comments

Comments
 (0)