File tree Expand file tree Collapse file tree 2 files changed +11
-23
lines changed Expand file tree Collapse file tree 2 files changed +11
-23
lines changed Original file line number Diff line number Diff line change 72
72
## String
73
73
| Title | Solution | Difficulty | Time | Space |
74
74
| ----- | -------- | ---------- | ---- | ----- |
75
- [ 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)|
77
- [ Keyboard Row] ( https://leetcode.com/problems/keyboard-row/ ) | [ Swift] ( ./KeyboardRow.swift ) | Easy| O(nm)| O(n)|
75
+ [ Fizz Buzz] ( https://leetcode.com/problems/fizz-buzz/ ) | [ Swift] ( ./String/ 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] ( ./String/ FirstUniqueCharacterInString.swift ) | Easy| O(n)| O(1)|
77
+ [ Keyboard Row] ( https://leetcode.com/problems/keyboard-row/ ) | [ Swift] ( ./String/ KeyboardRow.swift ) | Easy| O(nm)| O(n)|
78
78
[ Valid Palindrome] ( https://leetcode.com/problems/valid-palindrome/ ) | [ Swift] ( ./String/ValidPalindrome.swift ) | Easy| O(n)| O(n)|
79
79
[ Detect Capital] ( https://leetcode.com/problems/detect-capital/ ) | [ Swift] ( ./String/DetectCapital.swift ) | Easy| O(n)| O(1)|
80
80
[ Count and Say] ( https://leetcode.com/problems/count-and-say/ ) | [ Swift] ( ./String/CountAndSay.swift ) | Easy| O(n^2)| O(n)|
Original file line number Diff line number Diff line change 1
1
/**
2
2
* Question Link: https://leetcode.com/problems/keyboard-row/
3
- * Primary idea: Convert each row to set to determine the word is subset or not.
3
+ * Primary idea: Use filter to determine the word is subset or not.
4
4
*
5
5
* Note: You can also use intersect() or union() functions to solve this problem.
6
6
*
10
10
11
11
class KeyboardRow {
12
12
func findWords( _ words: [ String ] ) -> [ String ] {
13
- var res = [ String] ( )
14
-
15
- let rowOne = Set ( " qwertyuiop " . characters) , rowTwo = Set ( " asdfghjkl " . characters) , rowThree = Set ( " zxcvbnm " . characters)
16
-
17
- for word in words {
18
- if isInRow ( word, rowOne) || isInRow ( word, rowTwo) || isInRow ( word, rowThree) {
19
- res. append ( word)
20
- }
21
- }
22
-
23
- return res
13
+ let rowOne = " qwertyuiop " , rowTwo = " asdfghjkl " , rowThree = " zxcvbnm "
14
+
15
+ return words. filter { word in rowOne. contains ( word) || rowTwo. contains ( word) || rowThree. contains ( word) }
24
16
}
25
-
26
- fileprivate func isInRow( _ word: String , _ row: Set < Character > ) -> Bool {
27
- for char in word. lowercased ( ) . characters {
28
- if !row. contains ( char) {
29
- return false
30
- }
17
+
18
+ extension String {
19
+ func contains( _ word: String ) -> Bool {
20
+ return word. filter { c in !self . contains ( c) } . characters. count == 0
31
21
}
32
-
33
- return true
34
22
}
35
23
}
You can’t perform that action at this time.
0 commit comments