Array & Hashing

74. Valid Anagram

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

Examples

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 handle such a case?

Dadas dos cadenas s y t, devuelve true si t es un anagrama de s, y false en caso contrario.

Ejemplos

Ejemplo 1

Entrada: s = "anagram", t = "nagaram"
Salida: true

Ejemplo 2

Entrada: s = "rat", t = "car"
Salida: false

Restricciones

  • 1 <= s.length, t.length <= 5 * 10^4
  • s y t consisten unicamente de letras minusculas del alfabeto ingles

Desafio adicional

Que pasaria si las entradas contienen caracteres Unicode? Como adaptarias tu solucion para manejar ese caso?

valid_anagrama.py
from collections import Counter

s: str = "antena"
t: str = "atenea"
if Counter(s) == Counter(t):
    print(True)
else:
    print(False)
Keyboard shortcuts
h Previous problem
l Next problem
Esc Back to index
? Toggle this help