Post

leetcode(리트코드)-791 Custom Sort String(PYTHON)

leetcode 791 - Custom Sort String 문제입니다.

1. 문제

https://leetcode.com/problems/custom-sort-string/


2. Input , Output


3. 분류 및 난이도

Medium 난이도 문제입니다.


4. 문제 해석

  • order와 str이 주어집니다.
  • order는 정렬을 할 때의 우선순위이고, str을 order의 우선순위에 맞게 재정렬하면 됩니다.

5. code

코드설명

  • 먼저, 결과 string에 우선순위의 카운트에 맞게 문자들을 더해줍니다.
  • str을 돌면서 dic에 저장되지 않은 문자는 우선순위가 중요하지 않으므로 그대로 res string에 추가합니다.

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
    def customSortString(self, order: str, str: str) -> str:
        counting = 0 
        res = ""
        dic = {}
        for word in (order):
            res += str.count(word) * word
            dic[word] = 1
        for word in str : 
            if word in dic : 
                continue
            else : 
                res += word
        print(res)
        return res
            
            

6. 결과 및 후기, 개선점

This post is licensed under CC BY 4.0 by the author.