From c6c3c7c7f351a229db4835e2d140f4c5e0b6c972 Mon Sep 17 00:00:00 2001 From: Vikram Bais Date: Tue, 13 Oct 2020 15:25:04 +0530 Subject: [PATCH] added python code for Reverse Words in a String code for Reverse Words in a String --- LeetCode/0151_Reverse_Words_in_a_String.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 LeetCode/0151_Reverse_Words_in_a_String.py diff --git a/LeetCode/0151_Reverse_Words_in_a_String.py b/LeetCode/0151_Reverse_Words_in_a_String.py new file mode 100644 index 0000000..6e624f4 --- /dev/null +++ b/LeetCode/0151_Reverse_Words_in_a_String.py @@ -0,0 +1,10 @@ +class Solution: + def reverseWords(self, s): + list_of_string = s.split(' ') + #appliying filter function to remove extraspaces from list + new_list = list(filter(lambda x : True if (x!="") else False, list_of_string)) + #taking reverse of output list + new_list.reverse() + #joining the elements if list + final_string = " ".join(str(k) for k in new_list) + return final_string