Haskell Program to Check if two strings are anagram


In Haskell we can check if given two strings are anagram or not using sort function and freqMap.

What is Anagram?

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

For example, the word "listen" is an anagram of the word "silent". Anagrams are often used in word play, puzzles, and other forms of entertainment.

Algorithm

  • Step 1 − The Data.List module is imported to use sort function.

  • Step 2 − The isAnagram function using sort function is defined

  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, the two strings are passed to the isAnagram function. The result of the function is used to print a message indicating whether the two strings are anagram or not.

  • Step 4 − The variables named, “str1” and “str2” are being initialized. It will hold the strings which is to be checked if they are anagram or not.

  • Step 5 − The result is printed to the console using ‘putStrLn’ statement after the function is called.

Example 1

In this example, the isAnagram function checks if two strings are anagrams of each other by sorting the strings and comparing the sorted strings for equality. The sort function from the Data.List library is used to sort the strings.

import Data.List

isAnagram :: String -> String -> Bool
isAnagram str1 str2 = sort str1 == sort str2

main :: IO ()
main = do
   let str1 = "listen"
   let str2 = "silent"
   if isAnagram str1 str2
      then putStrLn "The two strings are anagrams of each other."
      else putStrLn "The two strings are not anagrams of each other."

Output

The two strings are anagrams of each other.

Example 2

In this example, the isAnagram function creates frequency maps for both input strings using the freqMap function, and compares the frequency maps for equality. The freqMap function takes a string as input and returns a Map representing the frequency of each character in the string. The Map.fromListWith function is used to create the frequency map by creating a list of tuples representing each character and its frequency, and then passing the list to Map.fromListWith along with a combining function ((+)) that adds up the frequencies of characters that appear multiple times.

import Data.List
import Data.Map (Map)
import qualified Data.Map as Map

isAnagram :: String -> String -> Bool
isAnagram str1 str2 = freqMap str1 == freqMap str2
  where freqMap = Map.fromListWith (+) . map (\c -> (c, 1))

main :: IO ()
main = do
   let str1 = "listen"
   let str2 = "silent"
   if isAnagram str1 str2
      then putStrLn "The two strings are anagrams of each other."
      else putStrLn "The two strings are not anagrams of each other."

Output

The two strings are anagrams of each other.

Example 3

In this example, the isAnagram function compares the character counts of the two input strings. The charCount function takes a string as input and returns an array representing the count of each character in the string. The accumArray function is used to create the character count array by creating a list of tuples representing each character and its count, and then passing the list to accumArray along with an initial value of zero and a combining function ((+)) that adds up the counts of characters that appear multiple times.

import Data.Array

isAnagram :: String -> String -> Bool
isAnagram str1 str2 = str1CharCount == str2CharCount
  where str1CharCount = charCount str1
        str2CharCount = charCount str2
        charCount str = accumArray (+) 0 ('a', 'z') [(c, 1) | c <- str, c >= 'a' && c <= 'z']

main :: IO ()
main = do
   let str1 = "listen"
   let str2 = "silent"
   if isAnagram str1 str2
      then putStrLn "The two strings are anagrams of each other."
      else putStrLn "The two strings are not anagrams of each other."

Output

The two strings are anagrams of each other.

Conclusion

In Haskell, we can check if the two passed strings are anagrams or not by using sort function, frequency maps or by using character counts.

Updated on: 24-Apr-2023

245 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements