Given a string, toggle the case of each alphabetic character: lowercase becomes uppercase and uppercase becomes lowercase. Non-alphabetic characters remain unchanged.
Iterate through each character. If it's uppercase, convert to lowercase. If it's lowercase, convert to uppercase. Otherwise, keep it as-is. This requires checking the case of each character using language-specific case detection methods.
Time complexity is O(n) where n is the string length. Space complexity is O(n) for the result. Each character is processed exactly once.
Edge cases include empty strings, strings with no letters, mixed alphanumeric strings, and already toggled strings.
Example Input & Output
Uppercase to lowercase
Empty string
Toggle case of each letter
Non-letters unchanged
Lowercase to uppercase
Algorithm Flow

Solution Approach
Best Answers
class Solution {
public String solution(String s) {
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++){char c=s.charAt(i);
if(Character.isUpperCase(c))sb.append(Character.toLowerCase(c));
else if(Character.isLowerCase(c))sb.append(Character.toUpperCase(c));
else sb.append(c);
}return sb.toString();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
