Valid Anagram - codepath/compsci_guides GitHub Wiki

Unit 12 Session 2 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10-15 mins
  • 🛠️ Topics: Strings, Hashing

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • What is an anagram?
    • An anagram is when two strings contain the same characters, just arranged differently.
HAPPY CASE
Input: s = ""anagram"", t = ""nagaram""
Output: True
Explanation: Both strings contain the same characters in different orders.

Input: s = ""rat"", t = ""car""
Output: False
Explanation: These strings do not contain the same set of characters.
EDGE CASE
Input: s = """", t = """"
Output: True
Explanation: Two empty strings are considered anagrams.

Input: s = ""a"", t = ""aa""
Output: False
Explanation: Strings of different lengths can't be anagrams.

2: M-atch

Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.

For String Comparison problems, we want to consider the following approaches:

  • Sorting: Sorting both strings and checking if they are equal.
  • Hashing: Using a character count or dictionary to track occurrences of characters.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Compare the counts of characters between the two strings. If they match for every character, the strings are anagrams. If not, they are not anagrams.

1) Check if the lengths of `s` and `t` are the same. If not, return False.
2) Iterate over all lowercase letters from 'a' to 'z'.
3) For each character, count its occurrences in both `s` and `t`.
4) If any character has different counts in the two strings, return False.
5) If all counts match, return True.

⚠️ Common Mistakes

  • Forgetting to compare string lengths early can result in unnecessary computations.
  • Not accounting for edge cases like empty strings.

4: I-mplement

Implement the code to solve the algorithm.

def is_anagram(s, t):
    # If the lengths are not the same, they can't be anagrams
    if len(s) != len(t):
        return False
    
    # Check if both strings have the same count for each character 'a' to 'z'
    for char in 'abcdefghijklmnopqrstuvwxyz':
        if s.count(char) != t.count(char):
            return False
    
    return True

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Input: s = ""anagram"", t = ""nagaram""

    • Length check passes.
    • Each character's count matches between the two strings.
    • Output: True
  • Input: s = ""rat"", t = ""car""

    • Length check passes.
    • Counts of characters 'r' and 't' do not match.
    • Output: False

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume N is the length of the input strings.

  • Time Complexity: O(N * 26) or O(N) since we compare counts for every character.
  • Space Complexity: O(1) as we only store a fixed number of character counts.