Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
My Solution:
public class Solution { public String[] findWords(String[] words) { String q = "[qwertyuiop]+"; String a = "[asdfghjkl]+"; String z = "[zxcvbnm]+"; List<String> list = new ArrayList<String>(); for(int i = 0; i < words.length; i++){ if(words[i].toLowerCase().matches(q) || words[i].toLowerCase().matches(a) || words[i].toLowerCase().matches(z)){ list.add(words[i]); } } String[] arr = new String[list.size()]; return list.toArray(arr); } }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步