Advertisement
Guest User

Untitled

a guest
Nov 8th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1.  
  2. public class Solution {
  3.     private List<String> keyboardRows;
  4.     private StringBuilder wordsPossibleToBuildUsingKeyboardRows;
  5.  
  6.     public Solution() {
  7.         this.keyboardRows = new ArrayList<>();
  8.         keyboardRows.add("qwertyuiop");
  9.         keyboardRows.add("asdfghjkl");
  10.         keyboardRows.add("zxcvbnm");
  11.  
  12.         wordsPossibleToBuildUsingKeyboardRows = new StringBuilder();
  13.     }
  14.  
  15.     public String[] findWords(String[] words) {
  16.  
  17.  
  18.         for (String word:words) {
  19.  
  20.             for (String keyboardRow:keyboardRows) {
  21.  
  22.                 if(checkIfCanBuildAWordInAGivenRow(word,keyboardRow)){
  23.                    wordsPossibleToBuildUsingKeyboardRows.append(word+",");
  24.  
  25.                 }
  26.             }
  27.  
  28.         }
  29.  
  30.  
  31.  
  32.  
  33.  
  34.         if(wordsPossibleToBuildUsingKeyboardRows.toString().equals("")){
  35.  
  36.             return new String[]{};
  37.         }
  38.         else {
  39.             return wordsPossibleToBuildUsingKeyboardRows.toString().split(",");
  40.         }
  41.  
  42.     }
  43.  
  44.     private boolean checkIfCanBuildAWordInAGivenRow(String word, String keyboardRow) {
  45.         word = word.toLowerCase();
  46.         for(int i = 0 ; i<word.length();i++){
  47.             char  currentLetter = word.charAt(i);
  48.             if(!keyboardRow.contains(String.valueOf(currentLetter))){
  49.                 return false;
  50.             }
  51.         }
  52.  
  53.         return true;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement