본문 바로가기

두두의 알고리즘/문제

[기타] 릿코드 Easy 557 'Reverse Words in a String III' (Python)

728x90

<문제 링크>

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


<문제 풀이>

  1. 문자열의 띄어쓰기를 기준으로 각 단어들이 뒤집으면 된다.

<코드>

class Solution:
    def reverseWords(self, s: str) -> str:
        answer = ''
        for i in s.split():
            answer += (i[::-1] + ' ')
        return answer[:-1]