Guest User

Untitled

a guest
Oct 26th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class KeyboardRow {
  5.  
  6. public static boolean checkRow(String row, String input) {
  7. boolean result = false;
  8. // check for row
  9. for (int i = 0; i < input.length(); i++) {
  10. if ( (row.indexOf(input.charAt(i))) != -1 ){
  11. if (i == input.length()-1) {
  12. return true;
  13. }
  14. } else if ( (row.indexOf(input.charAt(i))) == -1 ){
  15. return false;
  16. }
  17. }
  18. return result;
  19. }
  20.  
  21. public static String[] findWords(String[] words) {
  22. List<String> result = new ArrayList<>();
  23. String row1 = "qwertyuiopQWERTYUIOP";
  24. String row2 = "asdfghjklASDFGHJKL";
  25. String row3 = "zxcvbnmZXCVBNM";
  26.  
  27. for (int i = 0; i < words.length; i++) {
  28. String currentString = words[i];
  29. if (checkRow(row1,currentString) || checkRow(row2,currentString) || checkRow(row3,currentString)){
  30. result.add(words[i]);
  31. }
  32. }
  33. return result.toArray(new String[0]);
  34. }
  35.  
  36. public static void main(String[] args) {
  37. String[] words = {"Hello", "Alaska", "Dad", "Peace"};
  38. String[] result = findWords(words);
  39. for (int i = 0; i < result.length; i++) {
  40. System.out.println(result[i]);
  41. }
  42. }
  43. }
Add Comment
Please, Sign In to add comment