- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# 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[0] is last element of s, do

chk := s[0]

while s and s[0] 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

## Example

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[0] == s[-1]: chk = s[0] while s and s[0] == chk: s.popleft() while s and s[-1] == chk: s.pop() return len(s) s = "aabccabba" print(solve(s))

## Input

"aabccabba"

## Output

3

- Related Articles
- 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 maximum difference of adjacent values after deleting k numbers in python
- Program to find longest subarray of 1s after deleting one element using Python
- Program to find minimum number colors remain after merging in Python
- Program to find minimum number of monotonous string groups in Python
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Program to find minimum possible maximum value after k operations in python
- Program to find length of longest substring with 1s in a binary string after one 0-flip in Python
- Program to find length of longest palindromic substring after single rotation in Python
- Program to find maximum binary string after change in python
- Program to find minimum deletions to make string balanced in Python