Skip to content

Instantly share code, notes, and snippets.

@luoheng23
Created October 25, 2019 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luoheng23/9efa2197a91b9dd221aef19298eca9d1 to your computer and use it in GitHub Desktop.
Save luoheng23/9efa2197a91b9dd221aef19298eca9d1 to your computer and use it in GitHub Desktop.
func isAlpha(c byte) bool {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
}
func toLowerOrUpper(c byte) byte {
if c >= 'a' && c <= 'z' {
return c - 'a' + 'A'
} else if c >= 'A' && c <= 'Z' {
return c - 'A' + 'a'
}
return c
}
func letterCasePermutation(S string) []string {
sumC := uint(0)
for i := 0; i < len(S); i++ {
if isAlpha(S[i]) {
sumC++
}
}
res := make([]string, 0)
s := make([]byte, len(S))
for i := 0; i < (1 << sumC); i++ {
cur := uint(0)
for j := 0; j < len(S); j++ {
if isAlpha(S[j]) {
if i&(1<<cur) != 0 {
s[j] = toLowerOrUpper(S[j])
} else {
s[j] = S[j]
}
cur++
} else {
s[j] = S[j]
}
}
res = append(res, string(s))
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment