forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request soapyigu#283 from peacemoon/fix_keyboardrow
Fix solution for KeyboardRow
- Loading branch information
Showing
1 changed file
with
10 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/keyboard-row/ | ||
* Primary idea: Use filter to determine the word is subset or not. | ||
* | ||
* | ||
* Note: You can also use intersect() or union() functions to solve this problem. | ||
* | ||
* Time Complexity: O(nm), Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class KeyboardRow { | ||
func findWords(_ words: [String]) -> [String] { | ||
let rowOne = "qwertyuiop", rowTwo = "asdfghjkl", rowThree = "zxcvbnm" | ||
func findWords(_ words: [String]) -> [String] { | ||
let rowOne = "qwertyuiop", rowTwo = "asdfghjkl", rowThree = "zxcvbnm" | ||
|
||
return words.filter { word in rowOne.contains(word) || rowTwo.contains(word) || rowThree.contains(word) } | ||
} | ||
return words.filter { word in rowOne.contains(word.lowercased()) || rowTwo.contains(word.lowercased()) || rowThree.contains(word.lowercased()) } | ||
} | ||
} | ||
|
||
extension String { | ||
func contains(_ word: String) -> Bool { | ||
return word.filter { c in !self.contains(c) }.characters.count == 0 | ||
} | ||
extension String { | ||
func contains(_ word: String) -> Bool { | ||
return word.filter { c in !self.contains(c) }.count == 0 | ||
} | ||
} | ||
} |