检查小写和大写字符的顺序是否相同
给定一个字符串str只包含小写和大写字母。任务是检查小写字符和大写字符是否分别遵循相同的顺序。
注意:如果一个字符在小写中出现多次,则相同字符在大写中的出现应该是相同的。
例子:
Input: str = "geeGkEEsKS"
Output: Yes
Lowercase = geeks, Uppercase = GEEKS
Input: str = "GeEkKg"
Output: No
Lowercase = ekg, Uppercase = GEK
方法:
- 遍历字符串。
- 将小写字母和大写字母分别存储在两个单独的字符串lowerStr和upperStr中。
- 将小写字符串转换为大写。
- 检查两个大写字符串是否相等。
- 如果相等则打印 Yes,否则打印 No。
以下是上述方法的实现:
C++
// C++ program to check if lowercase and
// uppercase characters are in same order
#include
using namespace std;
// Function to check if both the
// case follow the same order
bool isCheck(string str)
{
int len = str.length();
string lowerStr = "", upperStr = "";
// Traverse the string
for (int i = 0; i < len; i++) {
// Store both lowercase and uppercase
// in two different strings
if (str[i] >= 65 && str[i] <= 91)
upperStr = upperStr + str[i];
else
lowerStr = lowerStr + str[i];
}
// using transform() function and ::toupper in STL
transform(lowerStr.begin(), lowerStr.end(),
lowerStr.begin(), ::toupper);
return lowerStr == upperStr;
}
// Driver code
int main()
{
string str = "geeGkEEsKS";
isCheck(str) ? cout << "Yes"
: cout << "No";
return 0;
}
Java
//Java program to check if lowercase and
// uppercase characters are in same order
public class GFG {
//Function to check if both the
//case follow the same order
static boolean isCheck(String str)
{
int len = str.length();
String lowerStr = "", upperStr = "";
char[] str1 = str.toCharArray();
// Traverse the string
for (int i = 0; i < len; i++) {
// Store both lowercase and uppercase
// in two different strings
if ((int)(str1[i]) >= 65 && (int)str1[i] <= 91)
upperStr = upperStr + str1[i];
else
lowerStr = lowerStr + str1[i];
}
// transform lowerStr1 to upper
String transformStr = lowerStr.toUpperCase();
return(transformStr.equals(upperStr));
}
//Driver code
public static void main(String[] args) {
String str = "geeGkEEsKS";
if (isCheck(str))
System.out.println("Yes");
else
System.out.println("No");
}
}
Python 3
# Python 3 program to Check if lowercase and
# uppercase characters are in same order
# Function to check if both the
# case follow the same order
def isCheck(str) :
length = len(str)
lowerStr, upperStr = "", ""
# Traverse the string
for i in range(length) :
# Store both lowercase and
# uppercase in two different
# strings
if (ord(str[i]) >= 65 and
ord(str[i]) <= 91) :
upperStr = upperStr + str[i]
else :
lowerStr = lowerStr + str[i]
# transform lowerStr to uppercase
transformStr = lowerStr.upper()
return transformStr == upperStr
# Driver Code
if __name__ == "__main__" :
str = "geeGkEEsKS"
if isCheck(str) :
print("Yes")
else :
print("No")
# This code is contributed
# by ANKITRAI1
C#
// C# program to check if lowercase and
// uppercase characters are in same order
using System;
class GFG {
//Function to check if both the
//case follow the same order
static bool isCheck(string str)
{
int len = str.Length;
string lowerStr = "", upperStr = "";
char[] str1 = str.ToCharArray();
// Traverse the string
for (int i = 0; i < len; i++) {
// Store both lowercase and uppercase
// in two different strings
if ((int)(str1[i]) >= 65 && (int)str1[i] <= 91)
upperStr = upperStr + str1[i];
else
lowerStr = lowerStr + str1[i];
}
// transform lowerStr1 to upper
String transformStr = lowerStr.ToUpper();
return(transformStr.Equals(upperStr));
}
//Driver code
public static void Main(String[] args) {
String str = "geeGkEEsKS";
if (isCheck(str))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
Javascript
输出:
Yes
时间复杂度: O(len)
辅助空间: O(len),其中 len 是字符串的长度。