Given a string that has set of words and spaces, write a program to move all spaces to front of string, by traversing the string only once.
Input : str = "geeks for geeks" Output : ste = " geeksforgeeks" Input : str = "move these spaces to beginning" Output : str = " movethesespacestobeginning" There were four space characters in input, all of them should be shifted in front.
This problem has existing solution, please refer Move spaces to front of string in single traversal link.
We will solve this problem quickly in Python using List Comprehension.
- Traverse input string and create a string without any space character using list comprehension.
- Now to know how many space characters were there in original string just take a difference of length of original string and new string.
- Now create another string and append space characters at the beginning.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Move spaces to front of string in single traversal
- Inverting the Move to Front Transform
- Move To Front Data Transform Algorithm
- Python - Remove front K characters from each string in String List
- Python | Check for spaces in string
- Python | Remove spaces from a string
- Python | Remove unwanted spaces from string
- Python | String Split including spaces
- Python | Test if String contains Alphabets and Spaces
- Python - Ways to remove multiple empty spaces from string List
- Python | Convert tuple records to single string
- Python | Convert Character Matrix to single String
- Python | Convert String list to Joined Single element
- Execute a String of Code in Python
- String containing first letter of every word in a given string with spaces
- Remove spaces from a given string
- C++ Program to remove spaces from a string
- URLify a given string (Replace spaces is %20)
- Removing spaces from a string using Stringstream
- Remove extra spaces from a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.