Keyboard Row

Problem

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.
Keyboard Image

Example 1:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]

Note: You may use one character in the keyboard more than once. You may assume the input string will only contain letters of alphabet.

Solution

public class Solution {
    public String[] findWords(String[] words) {
        return Stream.of(words).filter(s -> s.toLowerCase().matches("[qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*")).toArray(String[]::new);
    }
}

Analysis

Art of regex
Notice that how we get stream first using Stream.of(words) then we use filter() method and apply our regex inside
At the end, we use toArray(String[]::new) to get the array of String as required

results matching ""

    No results matching ""