Problem Solving with Algorithms

반응형

leetcode.com/contest/weekly-contest-211

 


1624. Largest Substring Between Two Equal Characters

leetcode.com/problems/largest-substring-between-two-equal-characters/

 

문제를 잘읽자, 없으면 -1을 반환하라고 되있으니.

class Solution:
    def maxLengthBetweenEqualCharacters(self, s: str) -> int:
        dic = {}
        max_len = -1
        
        for i in range(len(s)):
            if s[i] in dic:
                max_len = max(max_len, i - dic[s[i]] - 1)
            else:
                dic[s[i]] = i
        return max_len

 

    def maxLengthBetweenEqualCharacters(self, s: str) -> int:
        maxLen, indices = -1, {}
        for i, c in enumerate(s):
            maxLen = max(maxLen, i - indices.setdefault(c, i + 1))
        return maxLen        

setdefault(c,  i + 1)

    def maxLengthBetweenEqualCharacters(self, s: str) -> int:
        return max(map(sub, count(), map({}.setdefault, s, count(1))))

1625. Lexicographically Smallest String After Applying Operations

 

leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/

 

 

Since the length of s is even, the total number of possible sequences is at most 10 * 10 * s.length.

Hide Hint 2

You can generate all possible sequences and take their minimum.

Hide Hint 3

Keep track of already generated sequences so they are not processed again.

 

파이썬 로테이트: rotate = s[b:] + s[:b]

 

제대로 된 해설을 볼 필요가 있지 않을까? 아래는 내코드

class Solution:
    seen = set()
    
    def findLexSmallestString(self, s: str, a: int, b: int) -> str:
        if s in self.seen:
            return s
        self.seen.add(s)
        
        def op1(s: str) -> str:
            lString = list(s)
            for i, x in enumerate(lString):
                if i%2:
                    lString[i] = str((int(x)+a)%10)
            return ''.join(lString)
        
        s1 = self.findLexSmallestString(op1(s), a, b)
        s2 = self.findLexSmallestString(s[b:] + s[:b], a, b)
        
        return min(s, s1, s2)

 

 


 

 

 

반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band