DEV Community

Tanuja V
Tanuja V

Posted on • Edited on

2 2 1

Letter Case Permutation | LeetCode | Java

class Solution {
    List<String> resList;

    public List<String> letterCasePermutation(String s) {
        resList = new ArrayList<>();
        StringBuilder sb = new StringBuilder(s);
        backTrack(s, sb, 0);
        return resList;
    }

    void backTrack(String s, StringBuilder sb, int index){
        if(index==s.length()){
            resList.add(sb.toString());
            return;
        }

        if(Character.isAlphabetic(s.charAt(index))){
            if(Character.isUpperCase(s.charAt(index)))
                sb.setCharAt(index, Character.toLowerCase(s.charAt(index)));
            else
                sb.setCharAt(index, Character.toUpperCase(s.charAt(index)));

            backTrack(s,sb,index+1);
            sb.setCharAt(index, s.charAt(index));
        }
         backTrack(s,sb,index+1);
    }
}
Enter fullscreen mode Exit fullscreen mode

Thanks for reading :)
Feel free to comment and like the post if you found it helpful
Follow for more 🤝 && Happy Coding 🚀

If you enjoy my content, support me by following me on my other socials:
https://linktr.ee/tanujav7

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs