Tutorialspoint
Problem
Solution
Submissions

Valid Anagram

Certification: Basic Level Accuracy: 0% Submissions: 0 Points: 5

Write a C program to determine if two strings are anagrams of each other. 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, "listen" and "silent" are anagrams.

Example 1
  • Input: s = "listen", t = "silent"
  • Output: true
  • Explanation:
    Step 1: We need to check if "listen" and "silent" are anagrams.
    Step 2: Both strings have the same length (6 characters).
    Step 3: We count the occurrences of each character in both strings.
    Step 4: "listen" has: l(1), i(1), s(1), t(1), e(1), n(1).
    Step 5: "silent" has: s(1), i(1), l(1), e(1), n(1), t(1).
    Step 6: The character counts are identical, so they are anagrams.
Example 2
  • Input: s = "hello", t = "world"
  • Output: false
  • Explanation:
    Step 1: We need to check if "hello" and "world" are anagrams.
    Step 2: Both strings have the same length (5 characters).
    Step 3: We count the occurrences of each character in both strings.
    Step 4: "hello" has: h(1), e(1), l(2), o(1).
    Step 5: "world" has: w(1), o(1), r(1), l(1), d(1).
    Step 6: The character counts are different, so they are not anagrams.
Constraints
  • The input strings contain only lowercase English letters (a-z)
  • 1 <= s.length, t.length <= 5 * 10^4
  • The solution should have a time complexity of O(n) where n is the length of the strings
  • The solution should have a space complexity of O(1) as there is a fixed number of possible characters (26)
StringseBaySnowflake
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • First, check if the lengths of both strings are equal. If not, they cannot be anagrams.
  • Create a frequency counter for each character in the first string.
  • Decrement the frequency counter for each character in the second string.
  • If any character's frequency is not zero at the end, the strings are not anagrams.
  • Use an array of size 26 to store the frequency of each character.
  • Alternatively, use a hash table if the character set is large or unknown.

Steps to solve by this approach:

 Step 1: Check if the lengths of both strings are equal (if not, return false immediately)

 Step 2: Create a frequency array of size 26 to count occurrences of each lowercase letter
 Step 3: Iterate through the first string and increment the frequency for each character
 Step 4: Iterate through the second string and decrement the frequency for each character
 Step 5: While decrementing, if any frequency becomes negative, immediately return false
 Step 6: Check if all frequencies are zero after processing both strings
 Step 7: If all frequencies are zero, the strings are anagrams, so return true

Submitted Code :