Write Python Program to Recursively Remove All Adjacent Duplicates29 Aug 2024 | 4 min read In this tutorial, we will write the Python program to remove all adjacent duplicate elements recursively. In this problem, we are given a string and we need to return output string that should not have any adjacent duplicate element. Let's understand the following example. Example - Let's understand the following example. Method - 1:Let's understand the following example. Example - Output: ay ac Explanation - In the above code, the remove_adjacent_duplicates() function takes a string as input and removes all adjacent duplicates from it. A stack is used to store non-duplicate characters. We iterate over each character in the string. If the stack is not empty and the current character is equal to the top element of the stack, it means we have found a duplicate character. In such cases, we remove the top element from the stack using the pop() method. If the current character is not a duplicate, we push it onto the stack using the append() method. After iterating through all the characters, we convert the stack to a string using the ''.join(stack) method and return the resulting string. Finally, we provide several strings where the remove_adjacent_duplicates() function is called with different strings, and the output is printed. Method - 2:Let's understand another approach. Example - Output: mpie lop Explanation - The remove_adjacent_duplicates() function takes a string s and a character ch as input and recursively removes all adjacent duplicates from the string. If the length of the string is less than or equal to 1, the function returns the string as is. The function iterates over the characters in the string using a while loop. If two adjacent characters are the same, it finds the range of consecutive duplicate characters. The function then recursively calls itself with the remaining part of the string after the consecutive duplicates. The function removes the consecutive duplicates from the current range and appends the remaining string after the duplicates to the updated string. If there are no consecutive duplicates, the function moves to the next character. Finally, the function returns the updated string with all the adjacent duplicates removed. We provides two test cases where the remove_adjacent_duplicates() function is called with different strings and characters, and the output is printed. Method - 4We can solve this problem using the regex and run a loop to find the character having length more than 1. Let's understand the following example. Example - Output: z Explanation - In the above code, the remove() function uses regular expressions to find groups of consecutive repeated characters in the string s. It iterates over the matches found using re.finditer and stores them in the groups list. It then checks the length of each group and appends it to the string s if the length is not greater than 1. The rremove() function repeatedly calls the remove function on the input string s until the length of the string s remains unchanged. It updates the previous string ps to store the previous value of s before each iteration. Finally, it returns the resulting string after removing all adjacent duplicates. |
In this tutorial, we will learn one of the interesting Python libraries named holidays, which determines holidays of a given year of a particular day. It returns the specific result of the given date. We only need to pass the country name. However, it can only...
4 min read
1. Python for Financial Investment Fundamentals [Udemy] Launch your profession with this Python for Finance Udemy Course. You will become an expert with the Python language for financial examination. It is a fascinating course to comprehend how Python is utilized for handling monetary estimations and stock portfolio...
8 min read
A feature vector is a quantifiable characteristic of a particular observable phenomena. A good example is the human category's height and weight characteristic because it can be seen and measured. Assuming that they will have a static or non-linear relationship, we often rely on computer features...
3 min read
This tutorial will teach you how to create a contact book application using Python, PyQt, and SQLite. This project will be an effective way to learn different coding skills, as it requires you to apply various techniques and encourages you to research topics during the development...
23 min read
In this problem, we will be given two linked lists. The linked lists will be merged at a node of both the linked lists, forming a Y-shaped list. We have to find the node at which the linked lists merge. Let us see some examples to understand...
16 min read
The scipy.stats.nbinom() describes the Negative Binomial Discrete Distribution. It is an instance of the rv_discrete class inherited from the generic methods. It completes the techniques by adding details specific to this distribution. The Probability Mass Function, which gives the total number of failures for Negative Binomial Discrete...
4 min read
What is PySpark DataFrame? A dataframe in PySpark is a collection of data that is grouped into columns. A DataFrame is similar to a relational table in SparkSQL. We can create the pyspark dataframe with different functions in SparkSession. PySpark MapType MapType in PySpark is a data type to...
4 min read
In this tutorial, we will write the Python program to search a string in the dictionary with the given prefix and a suffix. We are given an array, consisting of N string and Q queries in the form of two strings prefix and suffix. Our task...
5 min read
Python is one of the most popular and fastest-growing programming languages in the world. Python is a programming language that is utilized for many different things. You can use Python to develop web apps, mobile apps, desktop apps, test software, and even for hacking. It's a...
8 min read
In this tutorial, we will learn how to declare a global Variable in Python Program. What is a Global Variable? The global variables are the ones that are available both within and outside of any function, provided they are defined outside a function which is a global...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India