Question

In: Computer Science

A simple anagram finder program in Golang?

A simple anagram finder program in Golang?

Solutions

Expert Solution

  • Below is the detailed solution of the above problem in Golang with code and output.
  • For better understanding read the comments mentioned in the code.
  • The program will make you enter two strings and will tell you if both are anagrams or not.
  • CODE:

package main

// import packages
import "fmt"
import "reflect"

// driver function
func main() {
//string variables to take input from user
   var s1 string
   var s2 string
  
   //ask and input from user
   fmt.Println("Enter the first string")
   fmt.Scanf("%s", &s1)
   fmt.Println("Enter the second string")
   fmt.Scanf("%s", &s2)

//if both strings are of unequal length
   if len(s1) != len(s2) {
       fmt.Println("These strings are not anagrams.")
   }

   //create a map and store (key, value) pair as (character, frequency)
   map1 := make(map[string]int)

//traverse string s1
   for _, r := range s1 {
       //string(r) used to convert rune type to string type
       j := map1[string(r)]
       //if not present,then create it otherwise add into it
       if j == 0 {
           map1[string(r)] = 1
       }else {
           map1[string(r)] = j + 1
       }
      
      
      
   }
  
   //create a map and store (key, value) pair as (character, frequency)
map2 := make(map[string]int)
   //traverse string s2
   for _, r := range s2 {
       //string(r) used to convert rune type to string type
       j := map2[string(r)]
//if not present,then create it otherwise add into it
       if j == 0 {
           map2[string(r)] = 1
       }else {
           map2[string(r)] = j + 1
       }
      
   }

   // so if both strings are anagrams i.e, having same frequency of each character in any order the
   // hashmap for both will be equal so comparing hashmap we get,
   anagram := reflect.DeepEqual(map1, map2)
  
   //if anagram is true otherwise false
   if anagram {
       fmt.Println("These strings are anagrams.")
   }else {
       fmt.Println("These strings are not anagrams.")
   }
  
  
}

  • INPUT/OUTPUT:
  1. aab
    baa
    These strings are anagrams.
  2. aab
    bcc
    These strings are not anagrams.
  • Below is the screenshot of the code attached for better clarity ad understanding.

CODE

So if you have any doubt regarding this solution then please feel free to ask in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


Related Solutions

Problem 1: Recursive anagram finder please used Java please Write a program that will take a...
Problem 1: Recursive anagram finder please used Java please Write a program that will take a word and print out every combination of letters in that word. For example, "sam" would print out sam, sma, asm, ams, mas, msa (the ordering doesn't matter) input: cram output: cram crma carm camr cmra cmar rcam rcma racm ramc rmca rmac acrm acmr arcm armc amcr amrc mcra mcar mrca mrac macr marc Hints: Your recursive function should have no return value and...
USING PYTHON ONLY Part 3a: Prime Number Finder Write a program that prompts the user to...
USING PYTHON ONLY Part 3a: Prime Number Finder Write a program that prompts the user to enter in a postive number. Only accept positive numbers - if the user supplies a negative number or zero you should re-prompt them. Next, determine if the given number is a prime number. A prime number is a number that has no positive divisors other than 1 and itself. For example, 5 is prime because the only numbers that evenly divide into 5 are...
Write code in Python that does the following : An anagram is when the order of...
Write code in Python that does the following : An anagram is when the order of a word can be changed to create a new word (evil, live, and vile for example). Using the dictionary provided, find all of the anagram groups. Which words contain the most anagrams? How large is this group? Here is another example: Alert, alter, and later have 3 in their group. Use the provided dict.txt as your word list, solution must also finish in less...
Write a simple javascript program using express and node.js to create a simple webpage that can...
Write a simple javascript program using express and node.js to create a simple webpage that can lead to other pages within the program. if possible, Comment everything out so I could understand how every things works.  But basically, server should be hosted on localhost:8000 and should have a simple starting page. Maybe a welcome message, with at least two "links" that goes to a "homepage" and then a "exit" page.
Write a simple airline ticket reservation program. The program should display a menu with the following...
Write a simple airline ticket reservation program. The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list...
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT