Letter Case Permutation LeetCode Solution - The Coding Shala

Home >> LeetCode >> Letter Case Permutation

 In this post, we will learn how to solve LeetCode's Letter Case Permutation Problem and will implement its solution in Java.

Letter Case Permutation Problem

Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string.

Example 1:
Input: S = "a1b2"
Output: ["a1b2","a1B2","A1b2","A1B2"]

Example 2:
Input: S = "3z4"
Output: ["3z4","3Z4"]

Example 3:
Input: S = "12345"
Output: ["12345"]

LeetCode - Letter Case Permutation Java Solution

Approach 1

Finding all the possible strings. (iterative solution)

Java Program: 

class Solution {
    public List<String> letterCasePermutation(String S) {
        List<String> res = new ArrayList<>();
        res.add(S);
        for(int i=0; i<S.length(); i++) {
            if((S.charAt(i) >= 'a' && S.charAt(i) <= 'z')  || (S.charAt(i) >= 'A' && S.charAt(i) <= 'Z')) {
                char ch = S.charAt(i);
                String str = "";
                if(S.charAt(i) >= 'a' && S.charAt(i) <= 'z') {
                    str = Character.toString(ch).toUpperCase();
                }
                if(S.charAt(i) >= 'A' && S.charAt(i) <= 'Z') {
                    str = Character.toString(ch).toLowerCase();
                }
                
                int len = res.size();
                for(int j=0; j<len; j++) {
                    String temp = res.get(j);
                    if(i==0) {
                        String newStr = str + temp.substring(1, temp.length());
                        res.add(newStr);
                    } else if(i == temp.length()-1) {
                        String newStr = temp.substring(0, temp.length()-1) + str;
                        res.add(newStr);
                    } else {
                        String newStr = temp.substring(0, i) + str + temp.substring(i+1, temp.length());
                        res.add(newStr);
                    }
                }
            }
        }
        return res;
    }
}

Approach 2

Using Recursion.

Java Program: 

class Solution {
    
    public void helper(List<String> res, int index, char[] str) {
        if(index == str.length) {
            res.add(new String(str));
        } else {
            if(Character.isLetter(str[index])) {
                str[index] = Character.toUpperCase(str[index]);
                helper(res, index+1, str);
                str[index] = Character.toLowerCase(str[index]);
            }
            helper(res, index+1, str);
        }
        
    }
    
    public List<String> letterCasePermutation(String S) {
        List<String> res = new ArrayList<>();
        helper(res, 0, S.toCharArray());
        return res;
    }
}


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala