Modify a sentence by reversing order of occurrences of all Palindrome Words
Given a string S representing a sentence, the task is to reverse the order of all palindromic words present in the sentence.
Examples:
Input: S = “mom and dad went to eye hospital”
Output: eye and dad went to mom hospital
Explanation: All palindromic words present in the string are “mom”, “dad” and “eye“, in the order of their occurrence. Reversing the order of their occurrence generates the sequence {“eye”, “dad”, “mom”}. Therefore, the modified string is “eye and dad went to mom hospital”.Input : S = “wow it is next level”
Output: level it is wow.
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
Python3
|
eye and dad went to mom hospital
Time Complexity: O(N)
Auxiliary Space: O(1)
Original page link
Best Cool Tech Gadgets
Top favorite technology gadgets
0 comments:
Post a Comment