讨论数量:
package main
import (
"fmt"
"unicode"
)
func mixCase(str string) []string {
var result []string
for i, char := range str {
if unicode.IsLetter(char) {
lower := string(unicode.ToLower(char))
upper := string(unicode.ToUpper(char))
if i%2 == 0 {
result = append(result, lower+str[i+1:])
result = append(result, upper+str[i+1:])
} else {
result = append(result, str[:i]+upper+str[i+1:])
result = append(result, str[:i]+lower+str[i+1:])
}
} else {
result = append(result, string(char))
}
}
return result
}
func main() {
str := "Aa 中"
result := mixCase(str)
for _, s := range result {
fmt.Println(s)
}
}
package main
import "fmt"
func main() {
str := []rune("Aa 中")
var res []string
var f func(int, string)
f = func(i int, r string) {
if i == len(str) {
res = append(res, r)
return
}
f(i+1, r+string(str[i]))
if str[i] >= 'A' && str[i] <= 'Z' {
f(i+1, r+string(str[i]+32))
} else if str[i] >= 'a' && str[i] <= 'z' {
f(i+1, r+string(str[i]-32))
}
}
f(0, "")
fmt.Println(res)
}
我自己也写了一个,参考下还有优化空间没
package main
import "fmt"
var result []string
func main() {
monogram([]rune("中aB"), 0, "")
fmt.Println(result)
}
func monogram(s []rune, index int, temp string) []string {
if index >= len(s) {
result = append(result, temp)
return result
}
monogram(s, index+1, temp+string(s[index]))
if s[index] >= 'a' && s[index] <= 'z' {
result = monogram(s, index+1, temp+string(s[index]-32))
} else if s[index] >= 'A' && s[index] <= 'Z' {
result = monogram(s, index+1, temp+string(s[index]+32))
}
return result
}
推荐文章: