📌  相关文章
📜  检查是否可以使用同一行的qwerty小键盘打印字符串

📅  最后修改于: 2021-04-29 05:08:25             🧑  作者: Mango

给定字符串S ,任务是检查是否只能使用qwerty键盘的单行键入该字符串。

例子:

方法:想法是将qwerty键盘的同一行的字符存储到不同的哈希图中,以检查字符串的所有字符是否来自同一行。
下面是上述方法的实现:

C++
// C++ Program to check whether
// the string can be printed
// using same row of qwerty keypad
 
#include 
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 first_row
        = { '1', '2', '3', '4',
            '5', '6', '7', '8',
            '9', '0', '-', '=' };
    set second_row
        = { 'Q', 'W', 'E', 'R', 'T',
            'Y', 'U', 'I', 'O', 'P',
            '[', ']', 'q', 'w', 'e',
            'r', 't', 'y', 'u', 'i',
            'o', 'p' };
    set third_row
        = { 'A', 'S', 'D', 'F', 'G',
            'H', 'J', 'K', 'L', ';',
            ':', 'a', 's', 'd', 'f',
            'g', 'h', 'j', 'k', 'l' };
    set 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 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 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 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 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


Python3
# Python3 program to check whether
# the string can be printed
# using same row of qwerty keypad
 
# Function to find the row of the
# character in the qwerty keypad
def checkQwertyRow(x):
 
    # Sets to include the
    # characters from the
    # same row of the qwerty keypad
    first_row = ['1', '2', '3', '4',
                 '5', '6', '7', '8',
                 '9', '0', '-', '=']
 
    second_row = ['Q', 'W', 'E', 'R',
                  'T', 'Y', 'U', 'I',
                  'O', 'P', '[', ']',
                  'q', 'w', 'e', 'r',
                  't', 'y', 'u', 'i',
                  'o', 'p']
    third_row = ['A', 'S', 'D', 'F',
                 'G', 'H', 'J', 'K',
                 'L', ';', ':', 'a',
                 's', 'd', 'f', 'g',
                 'h', 'j', 'k', 'l']
    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
    elif(second_row.count(x) > 0):
        return 2
    elif(third_row.count(x) > 0):
        return 3
    elif(fourth_row.count(x) > 0):
        return 4
    return 0
 
# Function to check the 
# characters are from the
# same row of the qwerty keypad
def checkValidity(str):
   
    x = str[0]
    row = checkQwertyRow(x)
 
    for i in range(len(str)):
        x = str[i]
        if(row != checkQwertyRow(x)):
            return False
    return True
 
# Driver Code
str = "GeeksforGeeks"
 
if(checkValidity(str)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program to check whether
// the string can be printed
// using same row of qwerty keypad
using System;
using System.Collections.Generic;
 
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
    char[] first_row1 = { '1', '2', '3', '4',
                          '5', '6', '7', '8',
                          '9', '0', '-', '=' };
    HashSet first_row = new HashSet(
                              first_row1);
     
    char[] second_row1 = { 'Q', 'W', 'E', 'R', 'T',
                           'Y', 'U', 'I', 'O', 'P',
                           '[', ']', 'q', 'w', 'e',
                           'r', 't', 'y', 'u', 'i',
                           'o', 'p' };
    HashSet second_row = new HashSet(
                               second_row1);
     
    char[] third_row1 = { 'A', 'S', 'D', 'F', 'G',
                          'H', 'J', 'K', 'L', ';',
                          ':', 'a', 's', 'd', 'f',
                          'g', 'h', 'j', 'k', 'l' };
    HashSet third_row = new HashSet(
                              third_row1);
     
    char[] fourth_row1 = { 'Z', 'X', 'C', 'V', 'B',
                           'N', 'M', ',', '.', '/',
                           'z', 'x', 'c', 'v', 'b',
                           'n', 'm' };
    HashSet fourth_row = new HashSet(
                               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 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
public static void Main(String[] args)
{
    String str = "GeeksforGeeks";
 
    if (checkValidity(str))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by Amit Katiyar


输出:
No

时间复杂度: O(N)