00:00

Problem Statement

Input/Output Examples

Visual Explanation

Java strings are immutable, so the method copies the characters into a char[], changes the letters in place and builds a new string at the end. Each character is decided on its own, with no state carried between them.

Approaches

Approach 1 relies on the Character class. Approach 2 uses the ASCII table directly.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the length of the string. Both approaches touch every character once and must build a new string of the same length, so the output takes O(n) space.

Interview Tips and Recommended Approach

  • Remember that strings are immutable. You cannot change a character in place; work on a char[] or a StringBuilder.
  • Explain the XOR trick, then its limit. c ^ 32 works only for ASCII letters; applied to '1' it would produce a different symbol.
  • Prefer the Character methods in real code. They handle Unicode letters and make the intent obvious to a reader.
  • Test non-letters: digits, spaces, punctuation and an empty string must come back unchanged.

Recommended Approach: Character Class Methods

Why: it is correct for every Unicode letter, leaves non-letters untouched and is easy to read. Mention the XOR version as an ASCII-only optimisation rather than your main answer.