Skip to content

Instantly share code, notes, and snippets.

@r14152
Created August 12, 2021 15:27
Show Gist options
  • Save r14152/9552d13fdd168c4dfc49517f17969d19 to your computer and use it in GitHub Desktop.
Save r14152/9552d13fdd168c4dfc49517f17969d19 to your computer and use it in GitHub Desktop.
Two words are anagrams of one another if their letters can be rearranged to form the other word. Given a string, split it into two contiguous substrings of equal length. Determine the minimum number of characters to change to make the two substrings into anagrams of one another.
//anagram used to check
//hackerank solution: https://www.hackerrank.com/challenges/anagram/problem
func anagram(s string) int32 {
if len(s)%2 != 0 {
return -1
}
runeMap := make(map[rune]int,0)
runesWord := []rune(s)
//fmt.Printf("Runes: %v:%d:%d\n", runesWord,(len(runesWord)-1)/2,((len(runesWord)-1)/2)+1)
for i:=0;i<=(len(runesWord)-1)/2;i++{
if _,found := runeMap[runesWord[i]]; found {
runeMap[runesWord[i]] += 1
}else{
runeMap[runesWord[i]] = 1
}
}
var count int32
for j:= ((len(runesWord)-1)/2)+1; j<len(runesWord);j++{
if val,found := runeMap[runesWord[j]]; found {
if val == 1{
delete(runeMap,runesWord[j])
continue
}
runeMap[runesWord[j]] -= 1
}else{
count += 1
}
}
return count
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment