- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 longest awesome substring in Python

Suppose we have a numeric string s. As we know an awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome. We have to find the length of the maximum length awesome substring of s.

So, if the input is like s = "4353526", then the output will be 5 because "35352" is the longest awesome substring. we can make "35253" palindrome.

To solve this, we will follow these steps −

n := 0

pos_map := a map containing a key 0 and corresponding value is size of s

max_len := 1

for i in range size of s - 1 to 0, decrease by 1, do

n := n XOR (2^s[i])

if n is present in pos_map, then

max_len := maximum of max_len and pos_map[n]-i

for j in range 0 to 9, do

m := n XOR 2^j

if m is in pos_map, then

max_len := maximum of max_len and pos_map[m]-i

if n not in pos_map, then

pos_map[n] := i

return max_len

## Example

Let us see the following implementation to get better understanding

def solve(s): n = 0 pos_map = {0:len(s)} max_len = 1 for i in range(len(s)-1, -1, -1): n = n ^ (1 << int(s[i])) if n in pos_map: max_len = max(max_len, pos_map[n]-i) for j in range(10): m = n ^ (1 << j) if m in pos_map: max_len = max(max_len, pos_map[m]-i) if n not in pos_map: pos_map[n] = i return max_len s = "4353526" print(solve(s))

## Input

"4353526"

## Output

5

- Related Questions & Answers
- Program to find longest nice substring using Python
- Program to find length of longest palindromic substring in Python
- Program to find length of longest consecutively increasing substring in Python
- Program to find longest substring of all vowels in order in Python
- Program to find length of longest common substring in C++
- Program to find length of longest repeating substring in a string in Python
- Program to find length of longest palindromic substring after single rotation in Python
- Program to find length of longest substring with even vowel counts in Python
- Longest Palindromic Substring in Python
- Find longest consecutive letter and digit substring in Python
- Program to find length of longest substring which contains k distinct characters in Python
- Python Program to Find Longest Common Substring using Dynamic Programming with Bottom-Up Approach
- Program to find the length of longest substring which has two distinct elements in Python
- Longest Palindromic Substring
- SequenceMatcher in Python for Longest Common Substring.