Advertisement

Valid Anagram - LeetCode 242 Solution

Valid Anagram - Complete Solution Guide

Valid Anagram is LeetCode problem 242, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given two strings s and t , return true if t is an anagram of s , and false otherwise. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false Constraints: 1 <= s.length, t.length <= 5 * 10 4 s and t consist of lowercase English letters. Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

Detailed Explanation

The problem asks whether two given strings, `s` and `t`, are anagrams of each other. Anagrams are words or phrases formed by rearranging the letters of another word or phrase. For example, "anagram" and "nagaram" are anagrams. The input consists of two strings, `s` and `t`, containing only lowercase English letters. The output is a boolean value: `true` if `t` is an anagram of `s`, and `false` otherwise. The strings can have lengths between 1 and 50,000 characters.

Solution Approach

The provided solutions use different approaches to solve the problem. The Python solution elegantly uses the `sorted()` function to sort both strings and compares the sorted results. The Java and C solutions use an array to count character frequencies, ensuring constant-time access to counts. The C++ solution uses maps to achieve the same result, albeit with slightly higher overhead.

Step-by-Step Algorithm

  1. Step 1: Check if the lengths of strings `s` and `t` are different. If they are, return `false` because anagrams must have the same length.
  2. Step 2: (For Java, C, and implicitly Python): Iterate through the characters of string `s`. For each character, increment its corresponding count in the character count array/map.
  3. Step 3: (For Java, C): Iterate through the characters of string `t`. For each character, decrement its corresponding count in the character count array.
  4. Step 4: (For Java, C): Iterate through the character count array/map. If any count is not zero, return `false`. Otherwise, return `true`.
  5. Step 5: (For Python): Sort strings `s` and `t` using the `sorted()` function. Compare the sorted strings. If they are equal, return `true`; otherwise, return `false`.
  6. Step 6: (For C++): Use two maps to store the character counts for `s` and `t`. Compare the maps for equality. If equal, return `true`, otherwise `false`.

Key Insights

  • Insight 1: Anagrams have the same number of each character. This means we can count the occurrences of each character in both strings and compare the counts.
  • Insight 2: Using a hash table (or array in this case, since characters are limited) is efficient for counting character frequencies. Alternatively, sorting both strings allows for direct comparison.
  • Insight 3: A quick check for unequal string lengths can immediately eliminate many non-anagram cases.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, String, Sorting.

Companies

Asked at: Accenture, Affirm, American Express, Atlassian, BlackRock, Capgemini, Cognizant, ConsultAdd, Disney, EPAM Systems, Fidelity, Goldman Sachs, Google, Infosys, Mastercard, Nagarro, Nokia, Oracle, PayPal, Rokt, Siemens, Tech Mahindra, Tesla, Visa, Walmart Labs, Wipro, Yandex, Yelp, persistent systems, tcs.