Alternating Case Transformation

Easy
0/40
0 upvote

Problem statement

You are given a string that may contain a mix of uppercase letters, lowercase letters, digits, and symbols. Your task is to transform this string according to a specific case-alternating rule and print the result.


The transformation rule is as follows:


1) All characters in the output string must appear in the same order as in the input string.

2) The case of the alphabetic characters must alternate, starting with lowercase. The first letter should be lowercase, the second uppercase, the third lowercase, and so on.

3) Non-alphabetic characters (like digits, spaces, and symbols) should not be changed and should be kept in their original positions. These non-alphabetic characters do not affect the alternating case count of the letters.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only line of input contains a single string S.


Output Format:
Print a single line containing the transformed string.


Note:
The core of the problem is to maintain a counter that only increments when you encounter an alphabetic character. This counter will determine whether the current letter should be lowercase (if the count is even) or uppercase (if the count is odd).
Sample Input 1:
Hello World


Sample Output 1:
hElLo WoRlD


Explanation for Sample 1:
'H' is the 1st letter -> lowercase 'h'.
'e' is the 2nd letter -> uppercase 'E'.
'l' is the 3rd letter -> lowercase 'l'.
'l' is the 4th letter -> uppercase 'L'.
'o' is the 5th letter -> lowercase 'o'.
' ' is a space, it remains unchanged.
'W' is the 6th letter -> uppercase 'W'.
'o' is the 2nd letter -> uppercase 'o'.
'r' is the 3rd letter -> lowercase 'R'.
'l' is the 4th letter -> uppercase 'l'.
'd' is the 5th letter -> lowercase 'D'.


Sample Input 2:
Abc123DEF!


Sample Output 2:
aBc123DeF!


Explanation for Sample 2:
'A' is the 1st letter (counter=0, even) -> 'a'. Counter becomes 1.
'b' is the 2nd letter (counter=1, odd) -> 'B'. Counter becomes 2.
'c' is the 3rd letter (counter=2, even) -> 'c'. Counter becomes 3.
'1', '2', '3' are non-letters. They are kept as-is. Counter remains 3.
'D' is the 4th letter (counter=3, odd) -> 'D'. Counter becomes 4.
'E' is the 5th letter (counter=4, even) -> 'e'. Counter becomes 5.
'F' is the 6th letter (counter=5, odd) -> 'F'. Counter becomes 6.
'!' is a non-letter, kept as-is.
Final Result: aBc123DeF!. This is correct.


Expected Time Complexity:
The expected time complexity is O(N).


Constraints:
1 <= length of S <= 10^5
The string S can contain any standard ASCII characters.

Time Limit: 1 sec
Approaches (1)
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Alternating Case Transformation
Full screen
Console