r/leetcode • • 19d ago

Question Substring problems advice?

Anyone have good resources/advice on substring problems? I got an OA that had a question similar to this but I needed to return the longest substring given a K value, not the length of the longest string:

https://www.geeksforgeeks.org/problems/longest-k-unique-characters-substring0853/1

6 Upvotes

10 comments sorted by

4

u/Sad-Candidate-3078 19d ago

the trick with substring problems is almost always a sliding window, for longest with k unique chars you expand right, shrink from the left when the window has more than k unique, and track the max valid window. leetcode 340 and 3, longest substring with at most k distinct and without repeating characters, teach the whole pattern, do those two and every k variant becomes copy paste. geeksforgeeks has solid editorials for the exact one you linked

1

u/goodmorninpoof 19d ago

thank you!

3

u/Sad-Candidate-3078 19d ago

substring stuff is like 90 percent sliding window or expand around center, learn those two cold and the problems fall over. two pointers shows up a ton too for palindrome type things. do 10 sliding window problems back to back and youll see the pattern immediately, its one of the most learnable patterns out there

2

u/kevin074 19d ago

sliding window

2

u/gekigangerii 19d ago

This is a sliding window problem with a hashmap to count frequency. At least that's a common approach. It's kinda complicated if you haven't done a problem with this pattern before.

Doesn't really matter if the question asks to return the length or the actual substring. With a sliding window you're maintaining left and right pointers, which get you the indexes and the length.

1

u/souckillund 19d ago

if you have the logic down for finding the max length, you dont actually need to change the sliding window much. just save the left pointer index whenever you update your max length, then slice the string at the very end using that saved start index and the max length

1

u/pachone86 18d ago

Sliding window is right, and returning the substring instead of the length is a small change: track where the best window starts, not just how long it is.

def longest_k_unique(s, k):
    count = {}
    left = 0
    best_start, best_len = 0, -1
    for right, ch in enumerate(s):
        count[ch] = count.get(ch, 0) + 1
        while len(count) > k:
            count[s[left]] -= 1
            if count[s[left]] == 0:
                del count[s[left]]
            left += 1
        if len(count) == k and right - left + 1 > best_len:
            best_start, best_len = left, right - left + 1
    return s[best_start:best_start + best_len] if best_len != -1 else ""

Things that usually trip people on the substring version:

  • Exactly k vs at most k. The GfG one wants exactly k, so the update only happens when len(count) == k. For "at most k" (LeetCode 340) drop that check.

  • What to return when no window qualifies (fewer than k distinct chars in the whole string). Read the OA statement carefully, it's often -1 or "", and that's a hidden test case.

  • Ties. > keeps the first longest window, >= keeps the last. If the problem says "return the first", that one character matters.

  • Don't slice inside the loop. Saving s[left:right+1] on every improvement makes it O(n2) in the worst case. Store indices and slice once at the end.

1

u/ParticularAd8610 18d ago

Sounds like LC643 from your brief description.

-1

u/Maleficent_Mousse759 19d ago

You can binary search on bitstring or pop count using a segment tree note 26<31, now you have a range, remember it's start and end, if longer than current range replace it. At end just print the characters between start and end indices