Last active
October 9, 2017 16:17
-
-
Save cixuuz/76f1b85f6da24a76e04d3f7b62acb774 to your computer and use it in GitHub Desktop.
[500. Keyboard Row] #leetcode
This file contains hidden or 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
class Solution { | |
public String[] findWords(String[] words) { | |
return Stream.of(words).filter(s -> s.toLowerCase().matches("[qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*")).toArray(String[]::new); | |
} | |
} |
This file contains hidden or 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
class Solution(object): | |
//O(n*l*26) O(1) | |
def findWords(self, words): | |
""" | |
:type words: List[str] | |
:rtype: List[str] | |
""" | |
res = list() | |
for word in words: | |
row = -1 | |
for letter in word: | |
if letter.lower() in "qwertyuiop": | |
temp = 0 | |
elif letter.lower() in "asdfghjkl": | |
temp = 1 | |
else: | |
temp = 2 | |
if row != -1 and row != temp: | |
break | |
row = temp | |
else: | |
res.append(word) | |
return res | |
def findWords(self, words): | |
return filter(re.compile('(?i)([qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*)$').match, words) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment