Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0704cd7

Browse files
Add reverseWords function in reverse_words_in_a_string_iii.py
Implemented a solution to reverse words in a string.
1 parent 3101cd6 commit 0704cd7

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Problem: Reverse Words in a String III
2+
# Link: https://leetcode.com/problems/reverse-words-in-a-string-iii/description/
3+
# Tags: String, Two Pointers
4+
# Approach: Split the sentence by spaces, reverse each word individually,
5+
# and rejoin them with spaces.
6+
# Time Complexity: O(n)
7+
# Space Complexity: O(n)
8+
9+
10+
class Solution:
11+
def reverseWords(self, s):
12+
return " ".join(word[::-1] for word in s.split())

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /