본문 바로가기

두두의 알고리즘/문제

[해시] 릿코드 Medium 567 'Permutation in String' (Python)

728x90

<문제 링크>

https://leetcode.com/problems/permutation-in-string/

 

Permutation in String - LeetCode

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


<문제 풀이>

  1. 순서 상관없이 s2 내에 연속해서 s1의 값이 들어있으면 됨
  2. 그 값이 몇개 있는지만 확인하면 되므로 Counter 라이브러리 이용

<코드>

from collections import Counter

class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        for i in range(len(s2)-len(s1)+1):
            if Counter(s1) == Counter(s2[i:i+len(s1)]):
                return True

 

<고쳐야 할 점>

  • from collections import Counter 기억하기