Skip to content

Commit

Permalink
moved arrays solution to new file and copied back old solution. (thes…
Browse files Browse the repository at this point in the history
…e commits should be squashed)
  • Loading branch information
qpwo committed Jul 13, 2019
1 parent cef0851 commit 7f643f4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 31 deletions.
65 changes: 34 additions & 31 deletions anagram/anagram.go
Expand Up @@ -2,47 +2,50 @@ package anagram

import "strings"

func isSubset(s, S string) bool {
used := make([]bool, len(S))
OuterLoop:
for _, r := range s {
if r == ' ' {
func FindAnagrams(dictionary []string, word string) (result []string) {
word = normalize(word)
if len(word) == 0 {
return nil
}

charDir := parseCharDic(word)
for _, value := range dictionary {
ww := normalize(value)
// ignore exact match or empty word(not anagram)
if ww == word || len(ww) == 0 {
continue
}
for i, R := range S {
if !used[i] && r == R {
used[i] = true
continue OuterLoop
}

if compareDics(charDir, parseCharDic(ww)) {
result = append(result, value)
}
return false
}
return true
return result
}

func nonEmpty(s string) bool {
for _, r := range s {
if r != ' ' {
return true
}
}
return false
func normalize(s string) string {
return strings.Replace(strings.ToLower(s), " ", "", -1)
}

func isAnagram(s1, s2 string) bool {
s1 = strings.ToLower(s1)
s2 = strings.ToLower(s2)
return s1 != s2 && nonEmpty(s1) && nonEmpty(s2) && isSubset(s1, s2) && isSubset(s2, s1)
func parseCharDic(word string) (result map[rune]int) {
result = make(map[rune]int)
for _, char := range word {
result[char] = result[char] + 1
}

return result
}

// FindAnagrams returns all the anagrams of `word` in dictionary
func FindAnagrams(dictionary []string, word string) []string {
// assumes that words are short
result := make([]string, 0)
for _, w := range dictionary {
if isAnagram(word, w) {
result = append(result, w)
func compareDics(dic1, dic2 map[rune]int) bool {
if len(dic1) != len(dic2) {
return false
}

for key, value := range dic1 {
if dic2[key] != value {
return false
}
}
return result

return true
}
49 changes: 49 additions & 0 deletions anagram/anagram_arrays.go
@@ -0,0 +1,49 @@
package anagram

import "strings"

func isSubset(s, S string) bool {
used := make([]bool, len(S))
OuterLoop:
for _, r := range s {
if r == ' ' {
continue
}
for i, R := range S {
if !used[i] && r == R {
used[i] = true
continue OuterLoop
}
}
return false
}
return true
}

func nonEmpty(s string) bool {
for _, r := range s {
if r != ' ' {
return true
}
}
return false
}

func isAnagram(s1, s2 string) bool {
s1 = strings.ToLower(s1)
s2 = strings.ToLower(s2)
return s1 != s2 && nonEmpty(s1) && nonEmpty(s2) && isSubset(s1, s2) && isSubset(s2, s1)
}

// FindAnagrams returns all the anagrams of `word` in dictionary.
// Faster than a hashtable-based method for short words.
func FindAnagrams2(dictionary []string, word string) []string {
// assumes that words are short
result := make([]string, 0)
for _, w := range dictionary {
if isAnagram(word, w) {
result = append(result, w)
}
}
return result
}

0 comments on commit 7f643f4

Please sign in to comment.