From 5fd17dd9bbb8f24f6447166ef6c7489457e77d06 Mon Sep 17 00:00:00 2001 From: Openset Date: Mon, 31 Dec 2018 11:18:15 +0800 Subject: [PATCH] Add: groups_of_special_equivalent_strings --- .../groups_of_special_equivalent_strings.go | 12 +++++++ ...oups_of_special_equivalent_strings_test.go | 34 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/problems/groups-of-special-equivalent-strings/groups_of_special_equivalent_strings.go b/problems/groups-of-special-equivalent-strings/groups_of_special_equivalent_strings.go index 16c8fb29d..788158b0f 100644 --- a/problems/groups-of-special-equivalent-strings/groups_of_special_equivalent_strings.go +++ b/problems/groups-of-special-equivalent-strings/groups_of_special_equivalent_strings.go @@ -1 +1,13 @@ package groups_of_special_equivalent_strings + +func numSpecialEquivGroups(A []string) int { + m := make(map[[52]int]bool) + for _, s := range A { + var count [52]int + for i, char := range s { + count[int(char)-'a'+26*(i%2)]++ + } + m[count] = true + } + return len(m) +} diff --git a/problems/groups-of-special-equivalent-strings/groups_of_special_equivalent_strings_test.go b/problems/groups-of-special-equivalent-strings/groups_of_special_equivalent_strings_test.go index 16c8fb29d..1e6a9ef9c 100644 --- a/problems/groups-of-special-equivalent-strings/groups_of_special_equivalent_strings_test.go +++ b/problems/groups-of-special-equivalent-strings/groups_of_special_equivalent_strings_test.go @@ -1 +1,35 @@ package groups_of_special_equivalent_strings + +import "testing" + +type caseType struct { + input []string + expected int +} + +func TestNumSpecialEquivGroups(t *testing.T) { + tests := [...]caseType{ + { + input: []string{"a", "b", "c", "a", "c", "c"}, + expected: 3, + }, + { + input: []string{"aa", "bb", "ab", "ba"}, + expected: 4, + }, + { + input: []string{"abc", "acb", "bac", "bca", "cab", "cba"}, + expected: 3, + }, + { + input: []string{"abcd", "cdab", "adcb", "cbad"}, + expected: 1, + }, + } + for _, tc := range tests { + output := numSpecialEquivGroups(tc.input) + if output != tc.expected { + t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected) + } + } +}