Check whether the string can be printed using same row of qwerty keypad


Introduction

In this tutorial, we will check if an input string can be formed using characters in the same row of the Qwerty keypad. The task is to check whether a given string exists in a single row of the Qwerty keypad. To determine whether a string can be printed with the same row of the Qwerty keypad, all characters should be found in the same row. We implement an approach using set and unordered_set to solve this task. The characters of different rows are stored in different sets or unordered_sets. Compare the string characters to each stored row value.

Qwerty keypad is a standard keyboard for all computers, laptops, and printers. The characters on a Qwerty keypad are specially designed for fast typing.

Example 1

String = "qwerty"

Output

Yes, the string exists in the same row of the qwerty keypad.

Explanation

The input string is "qwerty", this string can be formed by the characters of the first row of the qwerty keypad. Each character of the input string exists in the same row. Hence, the string can be printed using the same row of the qwerty keypad.

Example 2

String = "hello"

Output

NO, the string does not exist in the same row of the qwerty keypad.

Explanation

The input string is "hello" and it cannot be printed using the same row of the qwerty keypad. The characters of the input string exist in different rows. Hence, the Output is No.

C++ Library Functions

  • Unordered_set − It is a data structure in C++. It is one type of set (ordered and unordered set) and it stores elements unordered (not in any sorted manner).

unordered_set <data_type> unordered_set_name;
  • set − It is a container to store different elements of a particular data type. A set stores elements in some sorted order and each element is unique due to its identifier. It does not allow duplicate elements.

set<data_type> set_name;
  • length() − It is a string class library function, defined in the <string> header file. It returns the length of the input string in bytes.

string_name.length();
  • set.count() − it is a predefined function of the set. It counts the number of times a value matches the stored elements of the set. It returns 1 when an element is found in the set, else it returns 0.

set_name.count();

Algorithm

  • Initialize the value of the input string.

  • Store the characters of each row in a different set.

  • Compare the string with each element of the set using the if-else condition.

  • If all the string characters exist in the same set the Output is Yes.

  • Else the Output is No.

  • Print the Output.

Example 1

We implement the problem statement of this tutorial in C++ using multiple sets. The characters of the same row are stored in one set and store elements in three different sets. Compare the characters of the input string with elements in each set.

#include <bits/stdc++.h>
using namespace std;
// user-defined function to check string exist in same row of the qwerty keypad
int checkString(char c){
   // storing element of the different row of the qwerty keypad in different sets
   set<char> row_1 = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=' };
   set<char> row_2 = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' };
   set<char> row_3 = { 'A', 'S', 'D', 'F', 'G','H', 'J', 'K', 'L', ';', ':', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' };
   set<char> row_4 = { 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
   if (row_1.count(c) > 0) 
      return 1;
   else if (row_2.count(c) > 0) 
      return 2;
   else if (row_3.count(c) > 0) 
      return 3;
   else if (row_4.count(c) > 0) 
      return 4;
   return 0;   
}
// Function to check the input string can match the same row characters of the qwerty keypad
bool checkStringExist(string s)  {
   char c = s[0];
   int rowValue = checkString(c);
   for (int x = 0; x < s.length(); x++)   {
      c = s[x];
      if (rowValue != checkString(c))
         return false;}
      return true; 
}
int main() {
   string s = "qwerty";
   if (checkStringExist(s))
      cout << "Yes, the string can be printed using the same row of the qwerty keypad.";
   else
      cout << "No, the string can be printed using the same row of the qwerty keypad.";
   return (0); 
}

Output

Yes, the string can be printed using the same row of the qwerty keypad.

Example 2

In this C++ implementation of the problem statement, we use multiple unordered_set to store the values of the different rows of the Qwerty keypad. All characters of the rows are in lowercase. If the string contains uppercase characters, convert it to lowercase.

#include <iostream>
#include <unordered_set>
#include <cctype>
using namespace std;

//user-defined function to check if string can be printed using the same roe of the qwerty keypad
bool checkString(const string& s) {
   // different unordered_set to store elements
   unordered_set<char> row1 = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
   unordered_set<char> row2 = {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
   unordered_set<char> row3 = {'z', 'x', 'c', 'v', 'b', 'n', 'm'};

   // identifying the row of the string characters
   char stringChar = tolower(s[0]);
   unordered_set<char>* rowValues;
   if (row1.count(stringChar))
      rowValues = &row1;
   else if (row2.count(stringChar))
      rowValues = &row2;
   else if (row3.count(stringChar))
      rowValues = &row3;
   else
      return false;

   // checking if all characters of the string exist in same row
   for (char c : s) {
      if (tolower(c) != stringChar && !rowValues->count(tolower(c)))
         return false;
   }

   return true;
}

//code controller
int main() {
   string s = "Qwert"; // Predefined string

   //calling function
   if (checkString(s))
      cout << "Yes, the string can be printed using the same row of the QWERTY keypad." << endl;
   else
      cout << "No, the string cannot be printed using the same row of the QWERTY keypad." << endl;

   return 0;
}

Output

Yes, the string can be printed using the same row of the QWERTY keypad.

Conclusion

We have reached the end of this tutorial. We have implemented an approach to check whether an input string can be printed using characters in the same row of the Qwerty keypad. A string can be printed from the same row of the Qwerty keypad only when all characters exist in one row. Store values for different rows in set and unordered_set and compare string characters. We used demonstrations to elaborate on the meaning of the problem statement of this tutorial.

Updated on: 29-Sep-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements