LeetCode75
151. Reverse Words in a String
Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
Example 1:
Input: s = "the sky is blue" Output: "blue is sky the"
Example 2:
Input: s = " hello world " Output: "world hello" Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: s = "a good example" Output: "example good a" Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
Constraints:
1 <= s.length <= 104s contains English letters (upper-case and lower-case), digits, and spaces ' '.s.ما یک رشته s داریم که شامل چندین کلمهی جداشده با فاصلهها (spaces) است.
هدف ما:
split())رشته را با استفاده از split() بر اساس فاصلهها به یک لیست از کلمات تبدیل میکنیم.reverse())لیست کلمات را برعکس میکنیم تا ترتیب موردنظر را بدست آوریم.join())از join() استفاده میکنیم تا کلمات را با یک فاصله به هم متصل کنیم.📌 کد پایتون (با کلاس Solution)
class Solution:
def reverseWords(self, s: str) -> str:
# تقسیم رشته به لیست کلمات (فضاهای اضافی حذف میشوند)
words = s.split()
# معکوس کردن لیست
words.reverse()
# پیوستن کلمات با یک فاصلهی واحد
return " ".join(words)

split()s.split() رشته را به لیستی از کلمات تقسیم میکند و فاصلههای اضافی را حذف میکند." hello world " → ["hello", "world"]"a good example" → ["a", "good", "example"]
reverse()لیست را معکوس میکنیم.["hello", "world"] → ["world", "hello"]["a", "good", "example"] → ["example", "good", "a"]
join()کلمات را با یک فاصله در کنار هم قرار میدهیم.["world", "hello"] → "world hello"["example", "good", "a"] → "example good a"
✅ پیچیدگی زمانی (O(n))
split() و join() هر دو O(n) هستند.reverse() هم در O(n) اجرا میشود.O(n) است.✅ پیچیدگی فضایی (O(n))
split() یک لیست جدید ایجاد میکند، به اندازه O(n) فضا مصرف میشود.📌 روش جایگزین (با split(" ") و strip()))
به جای split() که خودبهخود فاصلههای اضافی را حذف میکند، میتوانیم از split(" ") همراه با strip() استفاده کنیم.
📌 ایده:
1. ابتدا فاصلههای ابتدا و انتهای رشته را حذف کنیم (strip()).
2. سپس رشته را با split(" ") تقسیم کنیم (که باعث ایجاد لیستی شامل برخی " "های اضافی میشود).
3. فقط کلمات غیرخالی را نگه داریم.
4. معکوس کنیم و با یک فاصله join() کنیم.
📌 کد جایگزین:
class Solution:
def reverseWords(self, s: str) -> str:
# حذف فاصلههای اضافی و نگهداشتن کلمات معتبر
words = [word for word in s.strip().split(" ") if word]
# معکوس کردن لیست و پیوند با فاصله
return " ".join(reversed(words))
✅ strip()
" hello world " → خروجی: "hello world"✅ split(" ")
"a good example" → خروجی: ["a", "good", "", "", "example"]✅ فیلتر کردن کلمات غیرخالی (if word)
["a", "good", "example"]✅ reversed()
["example", "good", "a"]✅ join(" ")
"example good a"
✅ نتیجه:
هر دو روش کارآمدند، اما روش اول (split()) کوتاهتر و خواناتر است، درحالیکه روش دوم در شرایط خاص کنترل بیشتری بر نحوه پردازش فاصلهها میدهد.
علی برادر خدام خسروشاهی