Easy - Valid Anagram
https://leetcode.com/problems/valid-anagram/description/
Valid Anagram - LeetCode
Can you solve this real interview question? Valid Anagram - Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using
leetcode.com
문제 설명
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
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 such a case?
예시
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
풀이
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
table = [0, ] * 26
for s_letter in s:
i = ord(s_letter) - ord('a')
table[i] += 1
for t_letter in t:
i = ord(t_letter) - ord('a')
table[i] -= 1
return all(map(lambda x: x == 0, table))
알파벳 소문자라는 주어진 조건을 사용해서, 배열을 만들고 (해쉬맵을 사용해도 된다) 주어진 두 문자열이 아나그램인지 확인한다.