Given a string S, the task is to check whether the string can be typed using only a single row of the qwerty keyboard.

Examples:
Input: S = “Dad”
Output: Yes
Explanation:
Characters “D” and “a” are present in the same row of qwerty keypad. That is second Row.Input: S = “Mom”
Output: No
Explanation:
Characters “M” and “o” are not present in the same row of qwerty keypad.
Approach: The idea is to store the characters of the same row of the qwerty keypad into different hash-maps to check that all the characters of the string are from the same row.
Below is the implementation of the above approach:
C++
// C++ Program to check whether // the string can be printed // using same row of qwerty keypad #include <bits/stdc++.h> using namespace std; // Function to find the row of the // character in the qwerty keypad int checkQwertyRow(char x) { // Sets to include the characters // from the same row of the qwerty keypad set<char> first_row = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=' }; set<char> second_row = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' }; set<char> third_row = { 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', ':', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' }; set<char> fourth_row = { 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', 'z', 'x', 'c', 'v', 'b', 'n', 'm' }; // Condition to check the row of the // current character of the string if (first_row.count(x) > 0) { return 1; } else if (second_row.count(x) > 0) { return 2; } else if (third_row.count(x) > 0) { return 3; } else if (fourth_row.count(x) > 0) { return 4; } return 0; } // Function to check the characters are // from the same row of the qwerty keypad bool checkValidity(string str) { char x = str[0]; int row = checkQwertyRow(x); for (int i = 0; i < str.length(); i++) { x = str[i]; if (row != checkQwertyRow(x)) { return false; } } return true; } // Driver Code int main() { string str = "GeeksforGeeks"; if (checkValidity(str)) cout << "Yes"; else cout << "No"; return (0); } |
Java
// Java program to check whether // the string can be printed // using same row of qwerty keypad import java.util.*; class GFG{ // Function to find the row of the // character in the qwerty keypad static int checkQwertyRow(char x) { // Sets to include the characters // from the same row of the qwerty keypad Character[] first_row1 = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=' }; Set<Character> first_row = new HashSet<>( Arrays.asList(first_row1)); Character[] second_row1 = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' }; Set<Character> second_row = new HashSet<>( Arrays.asList(second_row1)); Character[] third_row1 = { 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', ':', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' }; Set<Character> third_row = new HashSet<>( Arrays.asList(third_row1)); Character[] fourth_row1 = { 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', 'z', 'x', 'c', 'v', 'b', 'n', 'm' }; Set<Character> fourth_row = new HashSet<>( Arrays.asList(fourth_row1)); // Condition to check the row of the // current character of the string if (first_row.contains(x)) { return 1; } else if (second_row.contains(x)) { return 2; } else if (third_row.contains(x)) { return 3; } else if (fourth_row.contains(x)) { return 4; } return 0; } // Function to check the characters are // from the same row of the qwerty keypad static boolean checkValidity(String str) { char x = str.charAt(0); int row = checkQwertyRow(x); for(int i = 0; i < str.length(); i++) { x = str.charAt(i); if (row != checkQwertyRow(x)) { return false; } } return true; } // Driver code public static void main(String[] args) { String str = "GeeksforGeeks"; if (checkValidity(str)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by offbeat |
No
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Mobile Numeric Keypad Problem
- Count of buttons pressed in a keypad mobile
- Convert a sentence into its equivalent mobile numeric keypad sequence
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Check if a string can become empty by recursively deleting a given sub-string
- Queries to check if string B exists as substring in string A
- Check if left and right shift of any string results into given string
- Check if a string can be obtained by rotating another string d places
- Check if any permutation of string is a K times repeated string
- Check if a string can be obtained by rotating another string 2 places
- Python | Check if a given string is binary string or not
- Check if a given string is a Reverse Bitonic String or not
- Check if a string is a scrambled form of another string
- Check if a string can be repeated to make another string
- Check if a string contains a palindromic sub-string of even length
- Check if the given string is shuffled substring of another string
- Check if a string can be formed from another string using given constraints
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : offbeat

