Created
August 12, 2021 15:27
-
-
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.
This file contains hidden or 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
| //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