-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve the "Test for palindromic permutations" problem
- Loading branch information
Showing
3 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2015, Peter Mrekaj. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE.txt file. | ||
|
||
package htables | ||
|
||
// CanFormPalindrome returns true if s can represent a palindrome. | ||
// The time complexity is O(n) and O(c) additional space is needed, where | ||
// c is the number of distinct characters in the string s. | ||
func CanFormPalindrome(s string) bool { | ||
if len(s) == 0 { | ||
return true | ||
} | ||
|
||
f := make(map[rune]int) | ||
for _, c := range s { | ||
f[c]++ | ||
} | ||
|
||
var odd int | ||
for _, cnt := range f { | ||
if cnt%2 != 0 { | ||
if odd++; odd != 1 { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2015, Peter Mrekaj. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE.txt file. | ||
|
||
package htables | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/mrekucci/epi/internal/epiutil" | ||
) | ||
|
||
func TestCanFormPalindrome(t *testing.T) { | ||
for _, test := range []struct { | ||
in string | ||
want bool | ||
}{ | ||
{"", true}, | ||
{"ab", false}, | ||
{"aab", true}, | ||
{"aaab", false}, | ||
{"aaabccc", false}, | ||
{"aaabcc", false}, | ||
{"edified", true}, | ||
{"界☺世", false}, | ||
{"世世☺", true}, | ||
} { | ||
if got := CanFormPalindrome(test.in); got != test.want { | ||
t.Errorf("CanFormPalindrome(%q) = %t; want %t", test.in, got, test.want) | ||
} | ||
} | ||
} | ||
|
||
func benchCanFormPalindrome(b *testing.B, size int) { | ||
b.StopTimer() | ||
s := epiutil.RandStr(size, "abcdefghijklmnopqrstuvwxyz", rand.NewSource(int64(size))) | ||
b.StartTimer() | ||
for i := 0; i < b.N; i++ { | ||
CanFormPalindrome(s) | ||
} | ||
} | ||
|
||
func BenchmarkCanFormPalindrome1e2(b *testing.B) { benchCanFormPalindrome(b, 1e2) } | ||
func BenchmarkCanFormPalindrome1e4(b *testing.B) { benchCanFormPalindrome(b, 1e4) } | ||
func BenchmarkCanFormPalindrome1e6(b *testing.B) { benchCanFormPalindrome(b, 1e6) } |