Remove duplicate characters in a string python – Python Program to Remove Adjacent Duplicate Characters from a String | How to Remove All Adjacent Duplicates from a String?

Remove duplicate characters in a string python: Are you looking for help to remove all the adjacent duplicate characters in a string? Then, this tutorial can be extremely helpful for you as we have compiled all about how to remove adjacent duplicate characters from a string in Python clearly. Refer to the Sample Programs for removing all adjacent duplicates from a string and the function used for doing so.

Remove All Adjacent Duplicates from a String in Python

Given a string, which contains duplicate characters the task is to remove the adjacent duplicate characters from the given string.

Examples:

Example 1:

Input:

given string =bteechhgeeeekkkkssss

Output:

given string before removing adjacent duplicate characters =  bteechhgeeeekkkkssss
given string without after adjacent duplicate characters =  btechgeks

Example 2:

Input:

given string ='appplussstoppperr'

Output:

given string before removing adjacent duplicate characters = appplussstoppperr
given string without after adjacent duplicate characters = aplustoper

How to Remove Adjacent Duplicate Characters from a String in Python?

Below is the full approach to remove the adjacent duplicate characters from the given string in Python.

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

1)Using For loop and If statements(Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Pass the given string to the remAdj function which accepts the given string as the argument and returns the modified string with no adjacent duplicates.
  • The aim is to loop through the string, comparing each character to the one before it. If the current character differs from the preceding one, it should be included in the resultant string; otherwise, it should be ignored. The Time Complexity of this approach is n, where n is the length of the input string, and no extra space is required.
  • Print the modified string.
  • The Exit of the Program.

Below is the implementation:

Python Program to Remove Adjacent Duplicate Characters from a String using for Loop and If Statements

# Function to remove adjacent duplicates characters from a string
def remAdj(givenstrng):
    # convert the given string to list using list() function
    charslist = list(givenstrng)
    prevele = None
    p = 0
    # Traverse the given string
    for chars in givenstrng:
        if prevele != chars:
            charslist[p] = chars
            prevele = chars
            p = p + 1
    # join the list which contains characters to string using join function and return it
    return ''.join(charslist[:p])


# Driver code
# Give the string as static input and store it in a variable.
givenstrng = "bteechhgeeeekkkkssss"
# printing the given string before removing adjacent duplicate characters
print('given string before removing adjacent duplicate characters = ', givenstrng)
# Pass the given string to the remAdj function which accepts
# the given string as the argument
# and returns the modified string with no adjacent duplicates.
modistring = remAdj(givenstrng)
# printing the given string after removing adjacent duplicate characters
print('given string without after adjacent duplicate characters = ', modistring)

Output:

given string before removing adjacent duplicate characters =  bteechhgeeeekkkkssss
given string without after adjacent duplicate characters =  btechgeks

2)Using For loop and If statements(User Input)

Approach:

  • Give the string as user input using the input() function.
  • Store it in a variable.
  • Pass the given string to the remAdj function which accepts the given string as the argument and returns the modified string with no adjacent duplicates.
  • The aim is to loop through the string, comparing each character to the one before it. If the current character differs from the preceding one, it should be included in the resultant string; otherwise, it should be ignored. The Time Complexity of this approach is n, where n is the length of the input string, and no extra space is required.
  • Print the modified string.
  • The Exit of the Program.

Below is the implementation:

Python Program to Remove Adjacent Duplicate Characters from a String using for Loop and If Statements(User Input)

# Function to remove adjacent duplicates characters from a string
def remAdj(givenstrng):
    # convert the given string to list using list() function
    charslist = list(givenstrng)
    prevele = None
    p = 0
    # Traverse the given string
    for chars in givenstrng:
        if prevele != chars:
            charslist[p] = chars
            prevele = chars
            p = p + 1
    # join the list which contains characters to string using join function and return it
    return ''.join(charslist[:p])


# Driver code
# Give the string as user input using the input() function.
# Store it in a variable.
givenstrng = input('Enter some random string = ')
# printing the given string before removing adjacent duplicate characters
print('given string before removing adjacent duplicate characters = ', givenstrng)
# Pass the given string to the remAdj function which accepts
# the given string as the argument
# and returns the modified string with no adjacent duplicates.
modistring = remAdj(givenstrng)
# printing the given string after removing adjacent duplicate characters
print('given string without after adjacent duplicate characters = ', modistring)

Output:

Enter some random string = appplussstoppperr
given string before removing adjacent duplicate characters = appplussstoppperr
given string without after adjacent duplicate characters = aplustoper

Related Programs: