Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 5. 22. 23:45

https://leetcode.com/problems/palindromic-substrings/

 

Palindromic Substrings - 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번 시도 실패, 2번 시도 성공)
그냥 정말 별생각 없이 짠 코드였음;

class Solution:
    def isPalindrom(self, s: str) -> bool:
        for i in range(len(s)//2):
            if s[i] != s[-(i+1)]:
                return False
        return True
    
    def countSubstrings(self, s: str) -> int:
        l = len(s)
        res = l
        for i in range(l):
            for j in range(i+1, l):
                res += 1 if self.isPalindrom(s[i:j+1]) else 0
        
        return res
반응형