DEV Community

Tanuja V
Tanuja V

Posted on • Updated on

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

Top comments (0)