-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0784-Letter-case-permutation.cs
More file actions
44 lines (35 loc) · 1.14 KB
/
0784-Letter-case-permutation.cs
File metadata and controls
44 lines (35 loc) · 1.14 KB
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
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0784.Letter_case_permutation
{
public class _0784_Letter_case_permutation
{
IList<string> res = new List<string>();
public IList<string> LetterCasePermutation(string s)
{
if (s == null || s.Length == 0) return res;
res.Add(s);
backtrack(s.ToCharArray(), 0);
return res;
}
private void backtrack(char[] str, int index)
{
if (index == str.Length) return;
bool lower = str[index] >= 'a' && str[index] <= 'z';
bool upper = str[index] >= 'A' && str[index] <= 'Z';
// is letter
if (lower || upper)
{
var temp = str[index];
backtrack(str, index + 1);
str[index] = lower ? (char)(str[index] - 32) : (char)(str[index] + 32);
backtrack(str, index + 1);
res.Add(new string(str));
str[index] = temp;
}
else // is digit
backtrack(str, index + 1);
}
}
}