Skip to content

Commit

Permalink
Merge pull request soapyigu#283 from peacemoon/fix_keyboardrow
Browse files Browse the repository at this point in the history
Fix solution for KeyboardRow
  • Loading branch information
soapyigu authored Jan 21, 2020
2 parents 55bdfbb + 58a723e commit 9251282
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions String/KeyboardRow.swift
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
}
}
}

0 comments on commit 9251282

Please sign in to comment.