문제

 

https://leetcode.com/problems/reverse-words-in-a-string-iii/

 

Reverse Words in a String III - 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

Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

문자열 s가 주어지면 공백과 초기 단어 순서를 유지하면서 문장 내 각 단어의 문자 순서를 반대로 뒤집는다.

 

예제 1

Input Output
s = "Let's take LeetCode contest" "s'teL ekat edoCteeL tsetnoc"

 

문제풀이 코드

class Solution:
    def reverseWords(self, s: str) -> str:
        return ' '.join([''.join(reversed(list(x))) for x in s.split(' ')])

1. 문자열 s를 띄어쓰기로 split한다

2. x를 리스트로 변환하고 reversed한다.

3. 뒤집어진 리스트를 다시 문자열로 바꾸고 각 단어를 띄어쓰기로 합쳐준다.

+ Recent posts