Anagram Check Program in Go

Anagram Check Program in Go

Introduction

This program demonstrates how to check if two strings are anagrams of each other in Go. Two strings are anagrams if they contain the same characters with the same frequencies, but possibly in a different order.

Go Program


package main

import (
    "fmt"
    "strings"
    "sort"
)

// isAnagram checks if two strings are anagrams of each other.
func isAnagram(str1, str2 string) bool {
    // Remove all white spaces and convert to lower case for case-insensitive comparison
    str1 = strings.ReplaceAll(str1, " ", "")
    str2 = strings.ReplaceAll(str2, " ", "")
    str1 = strings.ToLower(str1)
    str2 = strings.ToLower(str2)
    
    // If lengths are not equal, they cannot be anagrams
    if len(str1) != len(str2) {
        return false
    }
    
    // Convert strings to slices of runes (to handle Unicode characters correctly)
    str1Runes := []rune(str1)
    str2Runes := []rune(str2)
    
    // Sort the slices
    sort.Slice(str1Runes, func(i, j int) bool { return str1Runes[i] < str1Runes[j] })
    sort.Slice(str2Runes, func(i, j int) bool { return str2Runes[i] < str2Runes[j] })
    
    // Compare sorted slices
    for i := range str1Runes {
        if str1Runes[i] != str2Runes[i] {
            return false
        }
    }
    return true
}

func main() {
    // Example strings
    str1 := "Listen"
    str2 := "Silent"
    
    // Check if the strings are anagrams
    if isAnagram(str1, str2) {
        fmt.Printf("%q and %q are anagrams.\n", str1, str2)
    } else {
        fmt.Printf("%q and %q are not anagrams.\n", str1, str2)
    }
}

    

Explanation

The program consists of a main package with two functions:

  • isAnagram: This function takes two strings as input and returns a boolean indicating whether they are anagrams. It removes any white spaces and converts the strings to lowercase to ensure a case-insensitive comparison. It then converts the strings to slices of runes to handle Unicode characters, sorts the slices, and compares them element by element.
  • main: This is the entry point of the program. It defines two example strings, calls the isAnagram function to check if they are anagrams, and prints the result.

Key Points:

  • White spaces are removed, and the strings are converted to lowercase to ensure a fair comparison.
  • The lengths of the strings are compared first; if they are not equal, the strings cannot be anagrams.
  • Strings are converted to slices of runes to handle Unicode characters correctly.
  • Sorting the slices and comparing them ensures that the strings contain the same characters with the same frequencies.
  • The program prints whether the given strings are anagrams or not.


By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)