Anagram Number

Definition & Explanation

Check if two strings are anagrams by comparing the frequency of each character in both strings. If the frequencies match, the strings are considered anagrams. An example would be comparing the strings ‘listen’ and ‘silent’ where the order of characters differs, but the frequency of each character is the same.

Example:
Input: “listen”, “silent”
Output: The strings are anagrams.

Algorithm

  1. Initialize variables.
  2. Accept input strings.
  3. Calculate the frequencies of characters in both strings.
  4. If the frequencies do not match, set flag = 1.
  5. If flag = 0, the strings are anagrams; otherwise, they are not.
  6. Print the result.
C

Method 1 :

#include <stdio.h>

int main() {
    // Initializing variables and input strings.
    char str1[100] = "listen", str2[100] = "silent";
    int first[26] = {0}, second[26] = {0}, c = 0, flag = 0;

    // Calculate frequencies of characters in the first string.
    while (str1[c] != '\0') {
        first[str1[c] - 'a']++;
        c++;
    }

    c = 0;
    // Calculate frequencies of characters in the second string.
    while (str2[c] != '\0') {
        second[str2[c] - 'a']++;
        c++;
    }

    // Check if frequencies of both strings are the same.
    for (c = 0; c < 26; c++) {
        if (first[c] != second[c]) {
            flag = 1;
            break; // If a mismatch is found, no need to continue checking.
        }
    }

    // Print the result.
    if (flag == 0) {
        printf("\n%s and %s are Anagram Strings.", str1, str2);
    } else {
        printf("\n%s and %s are not Anagram Strings.", str1, str2);
    }

    return 0;
}

Output :

listen and silent are Anagram Strings.
C++

Method 1 :

#include <iostream>
#include <algorithm>

int main() {
    // Initializing variables and input strings.
    std::string str1 = "listen", str2 = "silent";

    // Sorting characters in both strings.
    std::sort(str1.begin(), str1.end());
    std::sort(str2.begin(), str2.end());

    // Checking if sorted strings are the same.
    if (str1 == str2) {
        std::cout << str1 << " and " << str2 << " are Anagram Strings." << std::endl;
    } else {
        std::cout << str1 << " and " << str2 << " are not Anagram Strings." << std::endl;
    }

    return 0;
}

Output :

listen and silent are Anagram Strings.
JAVA

Method 1 :

import java.util.Arrays;

public class AnagramCheck {
    public static void main(String[] args) {
        // Initializing variables and input strings.
        String str1 = "listen", str2 = "silent";

        // Converting strings to character arrays and sorting.
        char[] charArray1 = str1.toCharArray();
        char[] charArray2 = str2.toCharArray();
        Arrays.sort(charArray1);
        Arrays.sort(charArray2);

        // Checking if sorted character arrays are the same.
        if (Arrays.equals(charArray1, charArray2)) {
            System.out.println(str1 + " and " + str2 + " are Anagram Strings.");
        } else {
            System.out.println(str1 + " and " + str2 + " are not Anagram Strings.");
        }
    }
}

Output :

listen and silent are Anagram Strings.
Python

Method 1 :

# Initializing variables and input strings.
str1 = "listen"
str2 = "silent"

# Sorting characters in both strings.
sorted_str1 = ''.join(sorted(str1))
sorted_str2 = ''.join(sorted(str2))

# Checking if sorted strings are the same.
if sorted_str1 == sorted_str2:
    print(f"{str1} and {str2} are Anagram Strings.")
else:
    print(f"{str1} and {str2} are not Anagram Strings.")

Output :

listen and silent are Anagram Strings.