檢查字串是否可以使用qwerty鍵盤的同一行列印
簡介
在本教程中,我們將檢查輸入字串是否可以使用Qwerty鍵盤的同一行中的字元形成。任務是檢查給定的字串是否存在於Qwerty鍵盤的單行中。為了確定字串是否可以使用Qwerty鍵盤的同一行列印,所有字元都應該在同一行中找到。我們使用set和unordered_set實現了一種方法來解決此任務。不同行的字元儲存在不同的集合或無序集合中。將字串字元與每個儲存的行值進行比較。
Qwerty鍵盤是所有計算機、筆記型電腦和印表機的標準鍵盤。Qwerty鍵盤上的字元專門設計用於快速打字。
示例 1
String = "qwerty"
輸出
Yes, the string exists in the same row of the qwerty keypad.
解釋
輸入字串為“qwerty”,此字串可以透過qwerty鍵盤第一行的字元形成。輸入字串的每個字元都存在於同一行中。因此,可以使用qwerty鍵盤的同一行列印該字串。
示例 2
String = "hello"
輸出
NO, the string does not exist in the same row of the qwerty keypad.
解釋
輸入字串為“hello”,它無法使用qwerty鍵盤的同一行列印。輸入字串的字元存在於不同的行中。因此,輸出為否。
C++庫函式
Unordered_set - 它是C++中的一種資料結構。它是一種集合(有序和無序集合),它以無序的方式儲存元素(不是以任何排序方式)。
unordered_set <data_type> unordered_set_name;
set - 它是用於儲存特定資料型別的不同元素的容器。集合以某種排序順序儲存元素,並且由於其識別符號,每個元素都是唯一的。它不允許重複元素。
set<data_type> set_name;
length() - 它是字串類庫函式,在<string>標頭檔案中定義。它以位元組為單位返回輸入字串的長度。
string_name.length();
set.count() - 它是集合的預定義函式。它計算值與集合儲存的元素匹配的次數。當在集合中找到元素時,它返回1,否則返回0。
set_name.count();
演算法
初始化輸入字串的值。
將每行的字元儲存在不同的集合中。
使用if-else條件將字串與集合的每個元素進行比較。
如果所有字串字元都存在於同一集合中,則輸出為是。
否則輸出為否。
列印輸出。
示例 1
我們使用多個集合在C++中實現了本教程的問題陳述。同一行的字元儲存在一個集合中,並將元素儲存在三個不同的集合中。將輸入字串的字元與每個集合中的元素進行比較。
#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);
}
輸出
Yes, the string can be printed using the same row of the qwerty keypad.
示例 2
在本教程中C++實現的問題陳述中,我們使用多個unordered_set來儲存Qwerty鍵盤不同行的值。所有行的字元都小寫。如果字串包含大寫字元,則將其轉換為小寫。
#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;
}
輸出
Yes, the string can be printed using the same row of the QWERTY keypad.
結論
我們已經到達本教程的結尾。我們已經實現了一種方法來檢查輸入字串是否可以使用Qwerty鍵盤的同一行中的字元列印。只有當所有字元都存在於一行中時,才能從Qwerty鍵盤的同一行列印字串。將不同行的值儲存在集合和無序集合中,並比較字串字元。我們使用演示來詳細說明本教程的問題陳述的含義。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP