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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay