1. Description
Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
constraints :
- 1 <= s.length <= 5 * \(10^{4}\)
- s contains printable ASCII characters.
- s does not contain any leading or trailing spaces.
- There is at least one word in s.
- All the words in s are separated by a single space.
2. Algorithms
- Start
- Convert data type of s into list splitting by whitespace.
- Repeat the steps through the element of s.
- For each string in s, reverse string and store it again.
- Return the joined list.
- End
3. Codes
class Solution:
def reverseWords(self, s: str) -> str:
s = s.split(' ')
for i in range(len(s)) :
s[i] = s[i][::-1]
return ' '.join(s)
4. Conclusion
'LeetCode > Easy' 카테고리의 다른 글
561 Array Partition (0) | 2022.09.06 |
---|---|
559 Maximum Depth of N-ary Tree (0) | 2022.09.06 |
551 Student Attendance Record I (0) | 2022.09.05 |
543 Diameter of Binary Tree (0) | 2022.09.05 |
541 Reverse String II (0) | 2022.09.05 |