LeetCode 784 Letter Case Permutation | Recursion & Backtracking Java Solution
LeetCode 784 Letter Case Permutation | Recursion & Backtracking Java Solution

LeetCode 784 Letter Case Permutation | Recursion & Backtracking Java Solution

Learn how to solve LeetCode 784 using recursion and backtracking. Includes decision tree, Java code, and step-by-step explanation.

11 views
0
0
Listen to articleAudio version

Introduction

The Letter Case Permutation problem is a classic example of recursion and backtracking, often asked in coding interviews and frequently searched by learners preparing for platforms like LeetCode.

This problem helps in understanding:

  1. Decision-making at each step
  2. Recursive branching
  3. String manipulation

In this article, we’ll break down the intuition, visualize the decision process using your decision tree, and implement an efficient Java solution.

πŸ”— Problem Link

LeetCode: Letter Case Permutation

Problem Statement

Given a string s, you can transform each alphabet character into:

  1. Lowercase
  2. Uppercase

Digits remain unchanged.

πŸ‘‰ Return all possible strings formed by these transformations.

Examples

Example 1

Input:

s = "a1b2"

Output:

["a1b2","a1B2","A1b2","A1B2"]

Example 2

Input:

s = "3z4"

Output:

["3z4","3Z4"]

Key Insight

At each character:

  1. If it's a digit β†’ only one choice
  2. If it's a letter β†’ two choices:
lowercase OR uppercase

So total combinations:

2^(number of letters)

Intuition (Using Your Decision Tree)

For input: "a1b2"

Start from index 0:

""
/ \
"a" "A"
| |
"a1" "A1"
/ \ / \
"a1b" "a1B" "A1b" "A1B"
| | | |
"a1b2" "a1B2" "A1b2" "A1B2"

Understanding the Tree

  1. At 'a' β†’ branch into 'a' and 'A'
  2. '1' β†’ no branching (digit)
  3. 'b' β†’ again branching
  4. '2' β†’ no branching

πŸ“Œ Leaf nodes = final answers

Approach: Recursion + Backtracking

Idea

  1. Traverse the string character by character
  2. If digit β†’ move forward
  3. If letter β†’ branch into:
  4. lowercase
  5. uppercase

Java Code

import java.util.*;

class Solution {

// List to store all results
List<String> lis = new ArrayList<>();

public void solve(String s, int ind, String ans) {

// Base case: reached end of string
if (ind == s.length()) {
lis.add(ans); // store generated string
return;
}

char ch = s.charAt(ind);

// If character is a digit β†’ only one option
if (ch >= '0' && ch <= '9') {
solve(s, ind + 1, ans + ch);
} else {

// Choice 1: convert to lowercase
solve(s, ind + 1, ans + Character.toLowerCase(ch));

// Choice 2: convert to uppercase
solve(s, ind + 1, ans + Character.toUpperCase(ch));
}
}

public List<String> letterCasePermutation(String s) {
solve(s, 0, ""); // start recursion
return lis;
}
}

Step-by-Step Execution

For "a1b2":

  1. Start β†’ ""
  2. 'a' β†’ "a", "A"
  3. '1' β†’ "a1", "A1"
  4. 'b' β†’ "a1b", "a1B", "A1b", "A1B"
  5. '2' β†’ final strings

Complexity Analysis

  1. Time Complexity: O(2^n)
  2. (n = number of letters)
  3. Space Complexity: O(2^n)
  4. (for storing results)

Why This Approach Works

  1. Recursion explores all possibilities
  2. Each letter creates a branching point
  3. Digits pass through unchanged
  4. Backtracking ensures all combinations are generated

Key Takeaways

  1. This is a binary decision recursion problem
  2. Letters β†’ 2 choices
  3. Digits β†’ 1 choice
  4. Decision tree directly maps to recursion
  5. Pattern similar to:
  6. Subsets
  7. Permutations with conditions

When This Problem Is Asked

Common in:

  1. Coding interviews
  2. Recursion/backtracking rounds
  3. String manipulation problems

Conclusion

The Letter Case Permutation problem is a perfect example of how recursion can be used to explore all possible combinations efficiently.

Once the decision tree is clear, the implementation becomes straightforward. This pattern is widely used in many advanced problems, making it essential to master.

Frequently Asked Questions (FAQs)

1. Why don’t digits create branches?

Because they have only one valid form.

2. What is the main concept used?

Recursion with decision-making (backtracking).

3. Can this be solved iteratively?

Yes, using BFS or iterative expansion, but recursion is more intuitive.

Ai Assistant Kas
LeetCode 784 Letter Case Permutation | Recursion & Backtracking Java Solution | Kode$word