Created
October 25, 2019 06:40
-
-
Save luoheng23/9efa2197a91b9dd221aef19298eca9d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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