py_string_permutation_checker

Código comentado línea a línea y paso a paso:

Entregas: py_string_permutation_checker.py con string_permutation_checker(s1, s2). Rank 03 · nivel 4.

Qué pide (en palabras normales)

Subject (enunciado original)
Write a function that determines if two strings are permutations of each other.
Case sensitive. Whitespace and punctuation count as regular characters.
Empty strings are permutations of each other.

Expected file: py_string_permutation_checker.py
Function signature:
def string_permutation_checker(s1: str, s2: str) -> bool:

Examples:
string_permutation_checker("abc", "bca")
=> True
string_permutation_checker("abc", "def")
=> False
string_permutation_checker("listen", "silent")
=> True
string_permutation_checker("hello", "bello")
=> False
string_permutation_checker("", "")
=> True
string_permutation_checker("a", "")
=> False
string_permutation_checker("Abc", "abc")
=> False
string_permutation_checker("a gentleman","elegant man")
=> True

Solución: contar y descontar

if len(s1) != len(s2):
    return False
counts = {}
for ch in s1:
    counts[ch] = counts.get(ch, 0) + 1
for ch in s2:
    if counts.get(ch, 0) == 0:
        return False
    counts[ch] -= 1
return True

Cuento los de s1, descuento con s2. Como las longitudes son iguales, si nunca me quedo a cero de más, tampoco sobra nada. Lineal.

Alternativa de una línea

return sorted(s1) == sorted(s2)

Vale igual (aquí sorted no está prohibido). La de contar es la que te sirve cuando sí lo esté.

Trampas