본문 바로가기

카테고리 없음

LeetCode-242 Valid Anagram 파이썬 풀이

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제

두 문자열 s와 t가 애너그램인지 판단하는 문제입니다. 애너그램이란 두 문자열이 문자가 배치된 순서만 다르고 모두 동일한 문자인 문자열입니다.

풀이

1. 정렬한 문자열 s와 t가 동일한지 판별합니다.

2. 동일하면 True 다르면 False를 반환합니다. 

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if sorted(s) == sorted(t):
            return True
        else:
            return False