Algorithm : Programmers - 문자열 내림차순으로 배치하기

코일·2021년 12월 24일
0

algorithm

목록 보기
18/37
post-thumbnail

링크텍스트

👉 문제

🤔생각해보기

  1. for 문으로 배열에 넣기
  2. 유니코드로 정렬 - ord( ), chr( )
  3. 정렬할 때는 sort(reverse=True)
  4. join 으로 묶어버리기

👉 해결

def solution(s):
    arr =[]
    char = 0
    for i in range(len(s)):
        
       char = (ord(s[i]) - ord('a'))
       arr.append(chr(char + ord('a')))

    arr.sort(reverse=True)
    
    return ''.join(arr)

🐱‍👤모범답안

def solution(s):
    return ''.join(sorted(s, reverse=True))

Simple is the best .

profile
How do you get what you want?🤔🤔

0개의 댓글