← Home

1542. Find Longest Awesome Substring

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
dynamic programmingC++Markdown
154

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1542. Find Longest Awesome Substring, the solution in this repository is mainly a dynamic programming solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: dynamic programming, bit manipulation.

The notes already sitting in the source point us in the right direction:

  • Bitmask
  • https://leetcode.com/problems/find-longest-awesome-substring/discuss/779893/C%2B%2BJavaPython3-with-picture-(similar-to-1371)
  • time: O(Nk), k: #unique chars
  • space: O(2^k)

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are longestAwesome.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(Nk), k: #unique chars
  • Space: O(2^k)

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Bitmask
02//https://leetcode.com/problems/find-longest-awesome-substring/discuss/779893/C%2B%2BJavaPython3-with-picture-(similar-to-1371)
03//time: O(Nk), k: #unique chars
04//space: O(2^k)
05class Solution {
06public:
07    int longestAwesome(string s) {
08        int n = s.size();
09        int mask = 0;
10        int ans = 0;
11        //dp[mask]: the smallest index with that state(mask)
12        //n: a large, out-of-range number
13        vector<int> dp(1024, n);
14
15        /*
16        for a index i in which the mask is 0,
17        s[-1+1...i] is s[0...i],
18        that means s[0...i]'s all digits all have even counts
19        */
20        dp[0] = -1;
21
22        for(int i = 0; i < n; ++i){
23            /*
24            mask is a bitmask of length 10,
25            the ith bit corresponds to the digit i,
26            if the digit i's count is even,
27            then mask's ith bit will be 0,
28            o.w. it will be 1
29            */
30            mask ^= (1 << (s[i]-'0'));
31
32            /*
33            the exactly same state(mask) occurs at
34            dp[mask], which is j,
35            so s[j+1...i]'s digits all have even counts
36            */
37            ans = max(ans, i - dp[mask]);
38            /*
39            differ by one digit,
40            suppose dp[mask^(1<<d)] is j,
41            that means s[j+1...i]'s digits all have even counts,
42            except one(the digit d)
43            */
44            for(int d = 0; d <= 9; ++d){
45                ans = max(ans, i - dp[mask^(1<<d)]);
46            }
47
48            //the first index with that state(mask)
49            if(dp[mask] == n) dp[mask] = i;
50        }
51
52        return ans;
53    }
54};

Cost

Complexity

Time
O(Nk), k: #unique chars
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(2^k)
Auxiliary state plus the answer structure where the problem requires one.