Open In App

Python code to move spaces to front of string in single traversal

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. Examples:

Input  : str = "geeks for geeks"
Output : str = "  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

Approach 1:

  1. Traverse input string and create a string without any space character using list comprehension.
  2. Now to know how many space characters were there in original string just take a difference of length of original string and new string.
  3. Now create another string and append space characters at the beginning.

Implementation




# Function to move spaces to front of string
# in single traversal in Python
 
def moveSpaces(input):
     
    # Traverse string to create string without spaces
    noSpaces = [ch for ch in input if ch!=' ']
 
    # calculate number of spaces
    space= len(input) - len(noSpaces)
 
    # create result string with spaces
    result = ' '*space
 
    # concatenate spaces with string having no spaces
    result = '"'+result + ''.join(noSpaces)+'"'
    print (result)
 
# Driver program
if __name__ == "__main__":
    input = 'geeks for geeks'
    moveSpaces(input)

Output
"  geeksforgeeks"

Time Complexity: O(n)
Auxiliary Space: O(n)

Approach 2 : Using count() and replace() methods

Implementation




# Function to move spaces to front of string
# in single traversal in Python
 
def moveSpaces(input):
    c=input.count(' ')
    input=input.replace(' ','')
    input=' '*c+input
    print(input)
 
# Driver program
if __name__ == "__main__":
    input = 'geeks for geeks'
    moveSpaces(input)

Output
  geeksforgeeks

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), as the function creates a new string of length n (where n is the length of the input string) to store the modified string with spaces moved to the front. 

Approach 3 : Using operator.countOf() and replace() methods




# Function to move spaces to front of string
# in single traversal in Python
 
def moveSpaces(input):
    import operator
    c=operator.countOf(input,' ')
    input=input.replace(' ','')
    input=' '*c+input
    print(input)
 
# Driver program
if __name__ == "__main__":
    input = 'geeks for geeks'
    moveSpaces(input)

Output
  geeksforgeeks

Time Complexity : O(N)
Auxiliary Space : O(1)

Approach 5: Using join() and split() methods




def moveSpaces(input):
    words = input.split()
    modified_str = ''.join(words)
    num_spaces = len(input) - len(modified_str)
    modified_str = ' ' * num_spaces + modified_str
    return modified_str
 
# Driver program
if __name__ == "__main__":
    input_str = 'geeks for geeks'
    print(moveSpaces(input_str))

Output
  geeksforgeeks

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string (to store the list of words and the modified string).

Approach 6: Using regular expressions

Algorithm:




import re
 
def moveSpaces(input_str):
  # Replace all occurrences of space with empty string
  modified_str = re.sub(' ', '', input_str)
  # Get the number of spaces in the original string
  num_spaces = len(input_str) - len(modified_str)
  # Add the required number of spaces at the beginning of the modified string
  modified_str = ' ' * num_spaces + modified_str
 
  return modified_str
input_str = 'geeks for geeks'
print(moveSpaces(input_str))

Output
  geeksforgeeks

Time complexity: O(n), where n is the length of the input string. The regular expression re.sub() method used in this approach has a time complexity of O(n), where n is the length of the input string.

Space complexity: O(n), where n is the length of the input string. This is because the re.sub() method creates a new string with all spaces removed, which is stored in memory before being used to calculate the number of spaces in the original string and add the required number of spaces at the beginning of the modified string.


Article Tags :