-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution784.cs
45 lines (40 loc) · 1.23 KB
/
Solution784.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution784
{
/// <summary>
/// 784. Letter Case Permutation - Medium
/// <a href="https://leetcode.com/problems/letter-case-permutation">See the problem</a>
/// </summary>
public IList<string> LetterCasePermutation(string s)
{
var result = new List<string>();
var sb = new StringBuilder();
LetterCasePermutation(s, 0, sb, result);
return result;
}
private void LetterCasePermutation(string s, int index, StringBuilder sb, List<string> result)
{
if (index == s.Length)
{
result.Add(sb.ToString());
return;
}
if (char.IsDigit(s[index]))
{
sb.Append(s[index]);
LetterCasePermutation(s, index + 1, sb, result);
sb.Remove(sb.Length - 1, 1);
}
else
{
sb.Append(char.ToLower(s[index]));
LetterCasePermutation(s, index + 1, sb, result);
sb.Remove(sb.Length - 1, 1);
sb.Append(char.ToUpper(s[index]));
LetterCasePermutation(s, index + 1, sb, result);
sb.Remove(sb.Length - 1, 1);
}
}
}