Given two restaurant names s1 and s2, decide whether they are similar.
Two names are similar when they have the same length and s1 can be turned into an anagram of s2 by changing at most 2 of its characters. The comparison is case-insensitive, so uppercase and lowercase letters count as the same character. This is the k-anagram check with k = 2.
Return True if the two names are similar and False otherwise.
Function Signature
def are_similar_restaurants(s1: str, s2: str) -> bool:
Rules
-
Lowercase both names before comparing. Spaces and digits are ordinary characters and count like letters.
-
Names of different lengths are never similar.
-
A change replaces one character of
s1
with any other character. Position does not matter: after the changes,
s1
only has to be a rearrangement of
s2
.
-
Zero changes is allowed, so names that are already anagrams of each other (or equal) are similar.
Constraints
-
1 <= s1.length, s2.length <= 1000
-
s1
and
s2
consist of English letters, digits and spaces.
Examples
Input: s1 = "Burger King", s2 = "Murger Bing"
Output: True
After lowercasing, every character count matches except that s1 has one k that s2 lacks and s2 has one m that s1 lacks. Changing the k in s1 to m makes it a rearrangement of s2, which is 1 change.
Input: s1 = "Taco Bell", s2 = "bell taco"
Output: True
Both lowercase to the same multiset of characters (including the single space), so no change is needed even though almost every position differs.
Input: s1 = "Sushi Bar", s2 = "Sushi Den"
Output: False
The characters b, a and r of s1 do not appear in s2, so 3 changes are needed, which is more than 2.