Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Last active October 9, 2017 16:17
Show Gist options
  • Save cixuuz/76f1b85f6da24a76e04d3f7b62acb774 to your computer and use it in GitHub Desktop.
Save cixuuz/76f1b85f6da24a76e04d3f7b62acb774 to your computer and use it in GitHub Desktop.
[500. Keyboard Row] #leetcode
class Solution {
public String[] findWords(String[] words) {
return Stream.of(words).filter(s -> s.toLowerCase().matches("[qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*")).toArray(String[]::new);
}
}
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