Interview Bootcamp
Ask Author
Back to course home

0% completed

Vote For New Content
Debasis B
C# recursive DFS

Debasis B

Feb 2, 2025

// List to store all permutations List<string> permutations = new List<string>(); public List<string> findLetterCaseStringPermutations(string str) { // Begin the process find(currPermutation: str.ToCharArray(), index: 0); return permutations; } private void find(char[] currPermutation, int index) { // Base case: if index is out of bounds, add the current permutation to the list if (index == currPermutation.Length) { permutations.Add(string.Concat(currPermutation)); return; } // If character is letter, toggle case and find next permutation if (char.IsLetter(currPermutation[index])) { // Toggle case for the character at the current index currPermutation[index] = toggleCase(currPermutation[index]); // Recursively find the next permutation: with toggled case find(currPermutation, index + 1); // Backtrack: undo the toggle case for the character at the current index currPermutation[index] = toggleCase(currPermutation[index]); } // Recursively find the next permutation // For letter it will be untoggled find(currPermutation, index + 1); } private char toggleCase(char c) { return char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c); }

0

0

Comments
Comments