파비의 매일매일 공부기록

2023.02.04 Today's Challenge 본문

Problem Solving/LeetCode

2023.02.04 Today's Challenge

fabichoi 2023. 2. 4. 23:45

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

 

Permutation in String - LeetCode

Permutation in String - Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. In other words, return true if one of s1's permutations is the substring of s2.   Example 1: Input: s1 = "ab", s2 = "eidbaooo" Output:

leetcode.com

퍼뮤테이션 구하는 문제

class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        n1 = len(s1)
        n2 = len(s2)
        
        if n1 > n2:
            return False
        
        arr1 = [0] * 26
        arr2 = [0] * 26

        for ch in s1:
            arr1[ord(ch) - ord('a')] += 1
        
        for i in range(n2):
            arr2[ord(s2[i]) - ord('a')] += 1
            if i>= n1:
                arr2[ord(s2[i-n1]) - ord('a')] -= 1
            if arr1 == arr2:
                return True
        
        return False
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.02.06 Today's Challenge  (0) 2023.02.06
2023.02.05 Today's Challenge  (0) 2023.02.05
2023.02.03 Today's Challenge  (0) 2023.02.03
2023.02.02 Today's Challenge  (0) 2023.02.02
2023.02.01 Today's Challenge  (0) 2023.02.01
Comments