# Program to find longest awesome substring in Python

PythonServer Side ProgrammingProgramming

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