Check if lowercase and uppercase characters are in same order in Python


Suppose we have a string s with only lowercase or uppercase letters not numbers. We have to check whether both lowercase and uppercase letters follow the same order respectively or not. So, if a letter occurs more than once in lowercase then the occurrence of the same character in the uppercase will be same.

So, if the input is like s = "piPpIePE", then the output will be True, as occurrences of lowercase letters and uppercase letters are same, and they are in the same order in lowercase and uppercase also.

To solve this, we will follow these steps −

  • lowercase := blank string, uppercase := blank string
  • for i in range 0 to size of s - 1, do
    • if s[i] is uppercase letter, then
      • uppercase := uppercase concatenate s[i]
    • otherwise,
      • lowercase := lowercase concatenate s[i]
  • to_upper := convert lowercase to uppercase
  • return true when to_upper is same as uppercase otherwise false

Example

Let us see the following implementation to get better understanding −

 Live Demo

def solve(s) :
   lowercase = ""
   uppercase = ""
   for i in range(len(s)) :
      if ord(s[i]) >= 65 and ord(s[i]) <= 91 :
         uppercase += s[i]
      else :
         lowercase += s[i]
   to_upper = lowercase.upper()
   return to_upper == uppercase
s = "piPpIePE"
print(solve(s))

Input

"piPpIePE"

Output

True

Updated on: 19-Jan-2021

397 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements