Check if lowercase and uppercase characters are in same order - GeeksforGeeks

15 Feb, 2022
Given a string str containing only lower and uppercase alphabets. The task is to check if both lowercase characters and uppercase characters follow the same order respectively. Note: If a character occurs more than once in lowercase then the occurrence of the same character in the uppercase should be same.Examples:  
Input: str = "geeGkEEsKS"
Output: Yes
Lowercase = geeks, Uppercase = GEEKS

Input: str = "GeEkKg"
Output: No
Lowercase = ekg, Uppercase = GEK
 
Approach:  
Traverse the string.Store the lowercase alphabets and uppercase alphabets in two separate strings lowerStr and upperStr respectively.Convert lowercase string to uppercase.Check if both uppercase strings are equal or not.Print Yes if equal, else print No.
Traverse the string.
Store the lowercase alphabets and uppercase alphabets in two separate strings lowerStr and upperStr respectively.
Convert lowercase string to uppercase.
Check if both uppercase strings are equal or not.
Print Yes if equal, else print No.
Below is the implementation of above approach:  
C++
Java
Python 3
C#
Javascript
// C++ program to check if lowercase and// uppercase characters are in same order#include <bits/stdc++.h>using namespace std; // Function to check if both the// case follow the same orderbool 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 codeint main(){    string str = "geeGkEEsKS";     isCheck(str) ? cout << "Yes"                 : cout << "No";     return 0;}
//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 program to Check if lowercase and# uppercase characters are in same order # Function to check if both the# case follow the same orderdef 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 Codeif __name__ == "__main__" :     str = "geeGkEEsKS"         if isCheck(str) :        print("Yes")    else :        print("No") # This code is contributed# by ANKITRAI1
// C# program to check if lowercase and// uppercase characters are in same orderusing 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");    }}
<script> // Javascript program to check if lowercase and// uppercase characters are in same order // Function to check if both the// case follow the same orderfunction isCheck(str){     var len = str.length;    var lowerStr = "", upperStr = "";     // Traverse the string    for (var i = 0; i < len; i++) {         // Store both lowercase and uppercase        // in two different strings        if (str[i] >= 'A' && str[i] < 'a')            upperStr = upperStr + str[i];         else            lowerStr = lowerStr + str[i];    }     lowerStr = lowerStr.toUpperCase();    console.log(lowerStr);    return lowerStr === upperStr;} // Driver codevar str = "geeGkEEsKS";isCheck(str) ? document.write( "Yes")             : document.write( "No"); </script>
Yes
 
Time Complexity: O(len)
Auxiliary Space: O(len), where len is the length of the string.
ankthon
ukasp
Kirti_Mangal
noob2000
sweetyty
rohitsingh07052
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Data Types
Inline Functions in C++
Pure Virtual Functions and Abstract Classes in C++
Write a program to reverse an array or string
Write a program to print all permutations of a given string
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack
KMP Algorithm for Pattern Searching