Check if lowercase and uppercase characters are in same order
Given 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isCheck(string str)
{
int len = str.length();
string lowerStr = "" , upperStr = "" ;
for ( int i = 0; i < len; i++) {
if (str[i] >= 65 && str[i] <= 91)
upperStr = upperStr + str[i];
else
lowerStr = lowerStr + str[i];
}
transform(lowerStr.begin(), lowerStr.end(),
lowerStr.begin(), :: toupper );
return lowerStr == upperStr;
}
int main()
{
string str = "geeGkEEsKS" ;
isCheck(str) ? cout << "Yes"
: cout << "No" ;
return 0;
}
|
Java
public class GFG {
static boolean isCheck(String str)
{
int len = str.length();
String lowerStr = "" , upperStr = "" ;
char [] str1 = str.toCharArray();
for ( int i = 0 ; i < len; i++) {
if (( int )(str1[i]) >= 65 && ( int )str1[i] <= 91 )
upperStr = upperStr + str1[i];
else
lowerStr = lowerStr + str1[i];
}
String transformStr = lowerStr.toUpperCase();
return (transformStr.equals(upperStr));
}
public static void main(String[] args) {
String str = "geeGkEEsKS" ;
if (isCheck(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python 3
def isCheck( str ) :
length = len ( str )
lowerStr, upperStr = " ", " "
for i in range (length) :
if ( ord ( str [i]) > = 65 and
ord ( str [i]) < = 91 ) :
upperStr = upperStr + str [i]
else :
lowerStr = lowerStr + str [i]
transformStr = lowerStr.upper()
return transformStr = = upperStr
if __name__ = = "__main__" :
str = "geeGkEEsKS"
if isCheck( str ) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool isCheck( string str)
{
int len = str.Length;
string lowerStr = "" , upperStr = "" ;
char [] str1 = str.ToCharArray();
for ( int i = 0; i < len; i++) {
if (( int )(str1[i]) >= 65 && ( int )str1[i] <= 91)
upperStr = upperStr + str1[i];
else
lowerStr = lowerStr + str1[i];
}
String transformStr = lowerStr.ToUpper();
return (transformStr.Equals(upperStr));
}
public static void Main(String[] args) {
String str = "geeGkEEsKS" ;
if (isCheck(str))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isCheck(str)
{
var len = str.length;
var lowerStr = "" , upperStr = "" ;
for ( var i = 0; i < len; i++) {
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;
}
var str = "geeGkEEsKS" ;
isCheck(str) ? document.write( "Yes" )
: document.write( "No" );
</script>
|
Complexity Analysis:
- Time Complexity: O(len)
- Auxiliary Space: O(len), where len is the length of the string.
Last Updated :
02 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...