|
@@ -0,0 +1,32 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "sort"
|
|
|
+)
|
|
|
+
|
|
|
+func main() {
|
|
|
+ var s, t string
|
|
|
+
|
|
|
+ s = "anagram"
|
|
|
+ t = "nagaram"
|
|
|
+ fmt.Println(isAnagram(s, t))
|
|
|
+
|
|
|
+ s = "rat"
|
|
|
+ t = "car"
|
|
|
+ fmt.Println(isAnagram(s, t))
|
|
|
+}
|
|
|
+
|
|
|
+// 通过排序来实现
|
|
|
+func isAnagram(s string, t string) bool {
|
|
|
+ sBys := []byte(s)
|
|
|
+ tBys := []byte(t)
|
|
|
+
|
|
|
+ sort.Slice(sBys, func(i, j int) bool {
|
|
|
+ return sBys[i] < sBys[j]
|
|
|
+ })
|
|
|
+ sort.Slice(tBys, func(i, j int) bool {
|
|
|
+ return tBys[i] < tBys[j]
|
|
|
+ })
|
|
|
+ return string(sBys) == string(tBys)
|
|
|
+}
|