KatInterview

Character Removal Anagram Matching

StringsAuto-graded · 25 test cases
MediumAuto-gradedStringshash-mapcountinganagrams

Last updated

Tests string manipulation and character frequency analysis to determine if two strings can become anagrams through optional single-character removal.

You are given a list of test cases. Each test case consists of two lowercase strings.

For each pair of strings, determine whether they can be made into anagrams using the following operations:

  • From the first string, you may optionally remove all occurrences of exactly one character
  • From the second string, you may optionally remove all occurrences of exactly one character
  • You can also choose to remove nothing from either or both strings

Two strings are anagrams if they contain exactly the same characters with exactly the same frequencies (i.e., identical character multisets).

Return a list of boolean values, one for each test case, indicating whether the transformation is possible.

Constraints

  • 1 ≤ number of test cases ≤ 100
  • 1 ≤ length of each string ≤ 1000
  • All strings contain only lowercase English letters

Examples

Example 1

Input: can_match_anagrams([['safddadfs', 'famafmss']])

Output: [True]

Example 2

Input: can_match_anagrams([['abc', 'abc'], ['aabb', 'ab'], ['abc', 'def'], ['aab', 'abb']])

Output: [True, False, False, False]

Hints

Hint 1

Count character frequencies in both strings and identify which characters have mismatched counts.

Hint 2

If there are no mismatches, the strings are already anagrams. If there is exactly one mismatched character, removing it from both strings makes them match.

Hint 3

If there are exactly two mismatched characters, check whether one appears only in the first string and the other appears only in the second string.