📌  相关文章
📜  打印给定数组中的所有字符串,这些字符串可以使用QWERTY键盘的单行中的键进行键入

📅  最后修改于: 2021-05-17 04:39:32             🧑  作者: Mango

给定一个字符串数组arr [] ,该字符串数组由大小写字母组成的字符串组成,任务是打印给定数组中的所有字符串,这些字符串可以使用QWERTY键盘的单行中的键进行键入。

例子:

方法:可以使用哈希解决问题。这个想法是遍历数组,对于每个字符串,检查是否可以使用同一行的键键入字符串的所有字符。打印发现正确的字符串。请按照以下步骤解决问题:

  • 初始化一个映射,例如mp ,为每个字符存储键盘上存在该字符的键的行号。
  • 遍历数组并为每个字符串,检查字符串的所有字符在地图或没有分配给它相同的行数。如果发现为真,则打印字符串。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print all strings that
// can be typed using keys of a single
// row in a QWERTY Keyboard
void findWordsSameRow(vector& arr)
{
 
    // Stores row number of all possible
    // character of the strings
    unordered_map mp{
        { 'q', 1 }, { 'w', 1 }, { 'e', 1 }, { 'r', 1 },
        { 't', 1 }, { 'y', 1 }, { 'u', 1 }, { 'o', 1 },
        { 'p', 1 }, { 'i', 1 }, { 'a', 2 }, { 's', 2 },
        { 'd', 2 }, { 'f', 2 }, { 'g', 2 }, { 'h', 2 },
        { 'j', 2 }, { 'k', 2 }, { 'l', 2 }, { 'z', 3 },
        { 'x', 3 }, { 'c', 3 }, { 'v', 3 }, { 'b', 3 },
        { 'n', 3 }, { 'm', 3 }
    };
 
    // Traverse the array
    for (auto word : arr) {
 
        // If current string is
        // not an empty string
        if (!word.empty()) {
 
            // Sets true / false if a string
            // can be typed using keys of a
            // single row or not
            bool flag = true;
 
            // Stores row number of the first
            // character of current string
            int rowNum
                = mp[tolower(word[0])];
 
            // Stores length of word
            int M = word.length();
 
            // Traverse current string
            for (int i = 1; i < M; i++) {
 
                // If current character can't be
                // typed using keys of rowNum only
                if (mp[tolower(word[i])]
                    != rowNum) {
 
                    // Update flag
                    flag = false;
                    break;
                }
            }
 
            // If current string can be typed
            // using keys from rowNum only
            if (flag) {
 
                // Print the string
                cout << word << " ";
            }
        }
    }
}
 
// Driver Code
int main()
{
    vector words
        = { "Yeti", "Had",
            "GFG", "comment" };
    findWordsSameRow(words);
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to print all strings that
// can be typed using keys of a single
// row in a QWERTY Keyboard
static void findWordsSameRow(List arr)
{
     
    // Stores row number of all possible
    // character of the strings
    Map mp = new HashMap();
    mp.put('q', 1);
    mp.put('w', 1);
    mp.put('e', 1);
    mp.put('r', 1);
    mp.put('t', 1);
    mp.put('y', 1);
    mp.put('u', 1);
    mp.put('i', 1);
    mp.put('o', 1);
    mp.put('p', 1);
    mp.put('a', 2);
    mp.put('s', 2);
    mp.put('d', 2);
    mp.put('f', 2);
    mp.put('g', 2);
    mp.put('h', 2);
    mp.put('j', 2);
    mp.put('k', 2);
    mp.put('l', 2);
    mp.put('z', 3);
    mp.put('x', 3);
    mp.put('c', 3);
    mp.put('v', 3);
    mp.put('b', 3);
    mp.put('n', 3);
    mp.put('m', 3);
 
    // Traverse the array
    for(String word : arr)
    {
         
        // If current string is
        // not an empty string
        if (word.length() != 0)
        {
             
            // Sets true / false if a string
            // can be typed using keys of a
            // single row or not
            boolean flag = true;
 
            // Stores row number of the first
            // character of current string
            int rowNum = mp.get(
                Character.toLowerCase(word.charAt(0)));
 
            // Stores length of word
            int M = word.length();
 
            // Traverse current string
            for(int i = 1; i < M; i++)
            {
                 
                // If current character can't be
                // typed using keys of rowNum only
                if (mp.get(Character.toLowerCase(
                        word.charAt(i))) != rowNum)
                {
                     
                    // Update flag
                    flag = false;
                    break;
                }
            }
 
            // If current string can be typed
            // using keys from rowNum only
            if (flag)
            {
                 
                // Print the string
                System.out.print(word + " ");
            }
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    List words = Arrays.asList(
        "Yeti", "Had", "GFG", "comment" );
         
    findWordsSameRow(words);
}
}
 
// This code is contributed by jithin


Python3
# Python3 program to implement
# the above approach
 
# Function to print all strings that
# can be typed using keys of a single
# row in a QWERTY Keyboard
def findWordsSameRow(arr):
 
    # Stores row number of all possible
    # character of the strings
    mp = { 'q' : 1, 'w' : 1, 'e' : 1, 'r' : 1,
           't' : 1, 'y' : 1, 'u' : 1, 'o' : 1,
           'p' : 1, 'i' : 1, 'a' : 2, 's' : 2,
           'd' : 2, 'f' : 2, 'g' : 2, 'h' : 2,
           'j' : 2, 'k' : 2, 'l' : 2, 'z' : 3,
           'x' : 3, 'c' : 3, 'v' : 3, 'b' : 3,
           'n' : 3, 'm' : 3 }
 
    #  Traverse the array
    for word in arr:
 
        # If current string is
        # not an empty string
        if (len(word) != 0):
             
            # Sets true / false if a string
            # can be typed using keys of a
            # single row or not
            flag = True
             
            rowNum = mp[word[0].lower()]
             
            # Stores length of word
            M = len(word)
 
            # Traverse current string
            for i in range(1, M):
 
                # If current character can't be
                # typed using keys of rowNum only
                if (mp[word[i].lower()] != rowNum):
 
                    # Update flag
                    flag = False
                    break
 
            # If current string can be typed
            # using keys from rowNum only
            if (flag):
 
                # Print the string
                print(word, end = ' ')
 
# Driver Code
words = [ "Yeti", "Had", "GFG", "comment" ]
 
findWordsSameRow(words)
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG
{
     
// Function to print all strings that
// can be typed using keys of a single
// row in a QWERTY Keyboard
static void findWordsSameRow(List arr)
{
     
    // Stores row number of all possible
    // character of the strings
    Dictionary mp = new Dictionary();
    mp.Add('q', 1);
    mp.Add('w', 1);
    mp.Add('e', 1);
    mp.Add('r', 1);
    mp.Add('t', 1);
    mp.Add('y', 1);
    mp.Add('u', 1);
    mp.Add('i', 1);
    mp.Add('o', 1);
    mp.Add('p', 1);
    mp.Add('a', 2);
    mp.Add('s', 2);
    mp.Add('d', 2);
    mp.Add('f', 2);
    mp.Add('g', 2);
    mp.Add('h', 2);
    mp.Add('j', 2);
    mp.Add('k', 2);
    mp.Add('l', 2);
    mp.Add('z', 3);
    mp.Add('x', 3);
    mp.Add('c', 3);
    mp.Add('v', 3);
    mp.Add('b', 3);
    mp.Add('n', 3);
    mp.Add('m', 3);
 
    // Traverse the array
    foreach(string word in arr)
    {
         
        // If current string is
        // not an empty string
        if (word.Length != 0)
        {
             
            // Sets true / false if a string
            // can be typed using keys of a
            // single row or not
            bool flag = true;
             
            // Stores row number of the first
            // character of current string
            int rowNum = mp[ char.ToLower(word[0])];
 
            // Stores length of word
            int M = word.Length;
 
            // Traverse current string
            for(int i = 1; i < M; i++)
            {
                 
                // If current character can't be
                // typed using keys of rowNum only
                if (mp[Char.ToLower(word[i])] != rowNum)
                {
                     
                    // Update flag
                    flag = false;
                    break;
                }
            }
 
            // If current string can be typed
            // using keys from rowNum only
            if (flag)
            {
                 
                // Print the string
                Console.Write(word + " ");
            }
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    List words = new List( new string[] {
        "Yeti", "Had", "GFG", "comment" });
         
    findWordsSameRow(words);
}
}
 
// This code is contributed by chitranayal


输出:
Yeti Had GFG

时间复杂度: O(N * M),其中N和M分别表示字符串数和最长字符串的长度。
辅助空间: O(26)