Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- private List<String> keyboardRows;
- private StringBuilder wordsPossibleToBuildUsingKeyboardRows;
- public Solution() {
- this.keyboardRows = new ArrayList<>();
- keyboardRows.add("qwertyuiop");
- keyboardRows.add("asdfghjkl");
- keyboardRows.add("zxcvbnm");
- wordsPossibleToBuildUsingKeyboardRows = new StringBuilder();
- }
- public String[] findWords(String[] words) {
- for (String word:words) {
- for (String keyboardRow:keyboardRows) {
- if(checkIfCanBuildAWordInAGivenRow(word,keyboardRow)){
- wordsPossibleToBuildUsingKeyboardRows.append(word+",");
- }
- }
- }
- if(wordsPossibleToBuildUsingKeyboardRows.toString().equals("")){
- return new String[]{};
- }
- else {
- return wordsPossibleToBuildUsingKeyboardRows.toString().split(",");
- }
- }
- private boolean checkIfCanBuildAWordInAGivenRow(String word, String keyboardRow) {
- word = word.toLowerCase();
- for(int i = 0 ; i<word.length();i++){
- char currentLetter = word.charAt(i);
- if(!keyboardRow.contains(String.valueOf(currentLetter))){
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement