Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class KeyboardRow {
- public static boolean checkRow(String row, String input) {
- boolean result = false;
- // check for row
- for (int i = 0; i < input.length(); i++) {
- if ( (row.indexOf(input.charAt(i))) != -1 ){
- if (i == input.length()-1) {
- return true;
- }
- } else if ( (row.indexOf(input.charAt(i))) == -1 ){
- return false;
- }
- }
- return result;
- }
- public static String[] findWords(String[] words) {
- List<String> result = new ArrayList<>();
- String row1 = "qwertyuiopQWERTYUIOP";
- String row2 = "asdfghjklASDFGHJKL";
- String row3 = "zxcvbnmZXCVBNM";
- for (int i = 0; i < words.length; i++) {
- String currentString = words[i];
- if (checkRow(row1,currentString) || checkRow(row2,currentString) || checkRow(row3,currentString)){
- result.add(words[i]);
- }
- }
- return result.toArray(new String[0]);
- }
- public static void main(String[] args) {
- String[] words = {"Hello", "Alaska", "Dad", "Peace"};
- String[] result = findWords(words);
- for (int i = 0; i < result.length; i++) {
- System.out.println(result[i]);
- }
- }
- }
Add Comment
Please, Sign In to add comment