- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find minimum length of string after deleting similar ends in Python
Suppose we have a string s with only three characters 'a', 'b', and 'c'. We shall apply the following algorithm on the string any number of times −
Select a non-empty prefix from the s where all the characters in the prefix are same.
Select a non-empty suffix from the s where all the characters in the suffix are same.
The prefix and the suffix are disjoint.
The characters from the prefix and suffix must be the same.
Remove both the prefix and the suffix from s.
Finally, we have to find the minimum length of s after performing the above operation any number of times (possibly zero times).
So, if the input is like s = "aabccabba", then the output will be 3 because we at first we can select prefix = "aa" and suffix = "a", so that the string after removal is "bccabb", then select prefix = "b" and suffix "bb", so string after removal is "cca", which is of length 3.
To solve this, we will follow these steps −
s := make a queue with s
while size of s > 1 and s is last element of s, do
chk := s
while s and s are same, do
delete left element from s
while s is not empty and last element of s is same as chk, do
delete last element from s
return size of s
Let us see the following implementation to get better understanding −
from collections import deque def solve(s): s = deque(s) while len(s) > 1 and s == s[-1]: chk = s while s and s == chk: s.popleft() while s and s[-1] == chk: s.pop() return len(s) s = "aabccabba" print(solve(s))
- Program to find minimum amplitude after deleting KLength sublist in Python
- Program to find minimum amplitude after deleting K elements in Python
- Program to find string after deleting k consecutive duplicate characters in python
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
- Program to find minimum length of lossy Run-Length Encoding in Python
- Program to find longest subarray of 1s after deleting one element using Python
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Program to find length of longest palindromic substring after single rotation in Python
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Program to find minimum number colors remain after merging in Python
- Program to find length of concatenated string of unique characters in Python?
- Program to find min length of run-length encoding after removing at most k characters in Python
- Program to find minimum number of monotonous string groups in Python
- Find all possible substrings after deleting k characters in Python
- Program to find length of longest contiguously strictly increasing sublist after removal in Python