Talentd
FleetCode

1542. Find Longest Awesome Substring

Hard47.4% acceptance1 company

You are given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it a palindrome.

Return the length of the maximum length awesome substring of s.

Example 1:

Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.

Example 2:

Input: s = "12345678"
Output: 1

Example 3:

Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.

Constraints:

  • 1 <= s.length <= 105
  • s consists only of digits.

Asked at 1 company

Hints open one at a time

Hint 1
Given the character counts, under what conditions can a palindrome be formed ?
Hint 2
From left to right, use bitwise xor-operation to compute for any prefix the number of times modulo 2 of each digit. (mask ^= (1<<(s[i]-'0')).
Hint 3
Expected complexity is O(n*A) where A is the alphabet (10).
34:47
Still stuck?Read 2 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def longestAwesome(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"3242415"
Input
"12345678"
Input
"213123"
Your code is saved on this device.Solution