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
- Python Program to Check whether String Contains Unique Characters
- C++ : How to Find Duplicates in a Vector
- Python Program to Count Number of Lowercase Characters in a String
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:
# 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:
# 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:
- Python Program to Remove the Characters of Odd Index Values in a String
- Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String
- Python Program to Trim Whitespace From a String
- Python Program to Read a String from the User and Append it into a File
- Python Program to Create a Class in which One Method Accepts a String from the User and Another Prints it
- Python Program to Count Number of Lowercase Characters in a String
- Python Program to Calculate the Number of Words and the Number of Characters Present in a String