Golang Program to Check if two Strings are Anagram


In this tutorial, we will learn how to check if given two strings are anagrams of each other or not using Go programming language.

A word or phrase formed by rearranging the letters of a different word or phrase by using all the letters exactly once is known as an anagram.

Example  Roles anagram word is loser

Binary anagram word is brainy

The below example shows how to create an empty map using the built-in make () function. The make function allocates and initializes a hash map data structure and returns a map value that points to it. The specifics of that data structure are an implementation detail of the runtime and are not specified by the language itself. Go provides a built-in map type that implements a hash table. Maps are golang builtin datatype similar to the hash table which maps a key to a value. . Below is the format for a map: map[key_type]value_type. Both key_type and value_type can be of different type or same type. A map is an unordered and changeable collection that does not allow duplicates.

Syntax

map[KeyType]ValueType

where KeyType may be any type that is comparable and ValueType may be any type at all, including another map.

Example of a Go program code to check if the two strings are anagram by using two separate functions

Algorithm

  • Step 1 − Import the package fmt.

  • Step 2 − Create the function Anagram ().

  • Step 3 − Declare and initialize the string variables.

  • Step 4 − Calling and using the make () function to return a map.

  • Step 5 − Iterating map using for range loop to check anagram condition.

  • Step 6 − Start the function main ().

  • Step 7 − Calling the function Anagram () to check if the two strings are anagram.

  • Step 8 − Print the result using fmt.Println ().

Example

// GOLANG PROGRAM TO CHECK IF TWO STRINGS ARE ANAGRAMS package main // fmt package allows us to print anything on the screen. import "fmt" // Create the function Anagram () to check if two strings are anagram func Anagram(s string, t string) bool { // initializing the variables string1 := len(s) string2 := len(t) fmt.Println("Letter 1 =", s, "\nLetter 2 =", t) fmt.Println("Is it an Anagram ?") if string1 != string2 { return false } // create and initialize a map anagramMap // Using make () function anagramMap := make(map[string]int) // As we already know that make() function // always returns a map which is initialized // Iterating map using for range loop // Traverse the first string and increase the count of each character in the map for i := 0; i < string1; i++ { anagramMap[string(s[i])]++ } // Traverse the second string and decrease the count of each character in the map for i := 0; i < string2; i++ { anagramMap[string(t[i])]-- } // Traverse the first string again and if for any character the count // is non-zero in the map then return false for i := 0; i < string1; i++ { if anagramMap[string(s[i])] != 0 { // if this condition satisfies return false return false } } // In the end return true return true } // start the function main () // GO program execution starts with the function main () func main() { fmt.Println("Golang program to to check if two strings are anagram") // Calling the function Anagram () to check if the two strings are anagram output := Anagram("listen", "silent") fmt.Println(output) // Calling the function Anagram () to check if the two strings are anagram output = Anagram("man", "name") fmt.Println(output) // print the result using the function fmt.Println () }

Output

Golang program to check if two strings are anagram

Golang program to to check if two strings are anagram
Letter 1 = listen
Letter 2 = silent
Is it an Anagram ?
true
Letter 1 = man
Letter 2 = name
Is it an Anagram ?
False

Description of the Code

  • In the above program code, we first declare the package main which tells compiler that the package should compile as an executable program instead of a shared library

  • We imported the fmt package that includes the files of package fmt. This package is all about formatting input and output

  • Next we create the function Anagram () to check if two given strings are anagram of each other.

  • Next we declare and initialize the string variables string1= len (s) and string2 := len (t).

  • We create and initialize a map anagramMap using built in make() function, which allocates and initializes a hash map data structure and returns a map value that points to it.

  • Next we use we use for loop to analyze the condition of the code and return a value

  • In the line 25  anagramMap [string (s[i])] ++: we traverse the first string and increase the count of each character in the map. In the line 30: anagramMap [string (t[i])] --: we traverse the second string and decrease the count of each character in the map.

  • In the line 36  if anagramMap [string (s [i])] != 0 {return false} : we traverse the first string again and if for any character the count is non-zero in the map then return false and in the end return true.

  • Now we start the function main () and this function is the entry point of the executable programs. It does not take any argument nor return anything

  • Under the function main (), we call the function Anagram () to check if the two strings are anagram of each other

  • Finally we print the result on the screen using fmt.Println () function.This function is defined under the fmt package and it helps to write standard output.

Conclusion

In the above example we have successfully compiled and executed the Go language program to check the two strings are anagram of each other.

Updated on: 15-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements