---
title: Maximum Feasible Substring under Pattern Supply
url: https://www.emergentmind.com/topics/maximum-feasible-substring-under-pattern-supply-mfsp
type: topic
---

# Maximum Feasible Substring under Pattern Supply

Maximum Feasible Substring under Pattern Supply (MFSP) is a combinatorial optimization problem arising in the context of frequency-based (Abelian/permutation/jumbled) pattern matching. Given a pattern $P$ and a text $T$ over a general alphabet $\Sigma$, MFSP seeks the longest substring of $T$ whose symbol counts are bounded, component-wise, by those of $P$. This exact resource-budget interpretation generalizes and connects standard Abelian pattern detection with packing-style primitives, and admits a linear-time solution via a two-pointer feasibility-maintenance algorithm [2601.09577].

## 1. Formal Problem Definition

Let $\Sigma$ be an alphabet of size $\sigma$. Given a pattern $P \in \Sigma^m$ and a text $T \in \Sigma^n$, denote the Parikh vector of a string $S$ by
\[
\mathrm{freq}_S(c) = |\{\,i: 0 \leq i < |S|,\ S[i]=c\}|
\]
for each $c \in \Sigma$. The pattern's Parikh vector $\mathrm{freq}_P$ is interpreted as a supply or budget vector.

A substring $S = T[\ell..r]$ is **feasible** if
\[
\mathrm{freq}_{T[\ell..r]}(c) \leq \mathrm{freq}_P(c)\quad\forall c \in \Sigma,
\]
or equivalently,
\[
\mathrm{freq}(T[\ell..r]) \preceq \mathrm{freq}(P).
\]
The Maximum Feasible Substring under Pattern Supply (MFSP) asks to find
\[
(\ell^*, r^*) \in \arg\max_{0 \leq \ell \leq r < n} (r - \ell + 1)\quad \text{subject to } \mathrm{freq}(T[\ell..r]) \preceq \mathrm{freq}(P).
\]
The goal is to output the substring $T[\ell^*..r^*]$, which has the maximum length $L^* = r^* - \ell^* + 1$ under the component-wise Parikh budget constraint.

## 2. Linear-Time Two-Pointer Feasibility Algorithm

The core algorithmic insight is that feasibility under the Parikh budget constraint is monotone under left-shrinkage: when a window $[\ell..r]$ becomes infeasible at some $r$, increasing $\ell$ can only reduce or restore feasibility. The solution maintains the following:

- $\mathrm{sup}[c] = \mathrm{freq}_P(c)$ for all $c \in \Sigma$,
- $\mathrm{cnt}[c] = \mathrm{freq}_{T[\ell..r]}(c)$ for all $c \in \Sigma$ (running counts for the current window),
- $\mathit{viol}$ = the number of $c$ for which $\mathrm{cnt}[c] > \mathrm{sup}[c]$ (violation counter).

For each iteration, after extending the right end $r$, the algorithm shrinks $\ell$ while $\mathit{viol} > 0$, ensuring each window $[\ell..r]$ is maximal and feasible. The substring of maximum feasible length is retained.

**Pseudocode outline:**

```c
Algorithm MFSP(T[0..n-1], P of length m):
    // 1. Build the supply vector
    for c in Σ: sup[c] ← 0
    for j = 0 to m-1: sup[P[j]] ← sup[P[j]] + 1

    // 2. Initialize sliding-window data
    for c in Σ: cnt[c] ← 0
    viol ← 0; ℓ ← 0; L* ← 0; (ℓ*, r*) ← (0, -1)

    // 3. Main iteration
    for r = 0 to n-1:
        c ← T[r]; old ← cnt[c]; cnt[c] ← old + 1
        if old ≤ sup[c] < cnt[c]: viol ← viol + 1

        // 4. Restore feasibility
        while viol > 0:
            d ← T[ℓ]; old_d ← cnt[d]; cnt[d] ← old_d - 1
            if sup[d] < old_d ≤ sup[d] + 1: viol ← viol - 1
            ℓ ← ℓ + 1

        // 5. Update longest feasible substring
        if (r - ℓ + 1) > L*:
            L* ← r - ℓ + 1; (ℓ*, r*) ← (ℓ, r)

    return (ℓ*, r*, L*)
```

## 3. Proof Sketch: Correctness and Complexity

- **Feasibility Invariant**: After the inner `while (viol > 0)`, $\mathit{viol} = 0$ and so $[\ell..r]$ is feasible; each counter $\mathrm{cnt}[c] \leq \mathrm{sup}[c]$ for all $c \in \Sigma$.
- **Window Maximality**: For each $r$, shrinking $\ell$ continues exactly while feasibility is violated, so $[\ell..r]$ is a longest feasible suffix ending at $r$.
- **Global Optimality**: For any optimal substring $(a, b)$, when $r = b$ the algorithm’s window will include a feasible window of length at least $b - a + 1$; the maximum seen is always retained.
- **Time and Space Complexity**: Each of pointers $r$, $\ell$ is advanced at most $n$ times. Array initializations and per-symbol updates are $O(1)$. Initialization over $\Sigma$ is $O(\sigma)$; total runtime is $O(n+\sigma)$. Space is $O(\sigma)$ for $\mathrm{sup}$, $\mathrm{cnt}$, and auxiliary counters.

## 4. Step-by-Step Example

Given $P = \text{"aab"}$ ($\mathrm{sup}(a)=2$, $\mathrm{sup}(b)=1$) and $T = \text{"abaacb"}$ ($n=6$):

| $r$   | Added char | $\mathrm{cnt}$ change       | $\mathit{viol}$ | Window $[\ell..r]$ | Remarks / $L^*$ |
|-------|------------|----------------------------|-----------------|--------------------|-----------------|
| 0     | 'a'        | $a\colon 0\to 1$           | 0               | $[0..0] = \text{"a"}$   | Set $L^*=1$     |
| 1     | 'b'        | $b\colon 0\to 1$           | 0               | $[0..1] = \text{"ab"}$  | Set $L^*=2$     |
| 2     | 'a'        | $a\colon 1\to 2$           | 0               | $[0..2] = \text{"aba"}$ | Set $L^*=3$     |
| 3     | 'a'        | $a\colon 2\to 3$           | 1               | shrink $\ell$: $[1..3] = \text{"baa"}$, $a\colon 3\to 2$, resolve violation | No update (tie) |
| 4     | 'c'        | $c\colon 0\to 1$           | 1               | shrink $\ell$: $[2..4] = \text{"aac"}$, $b\colon 1\to 0$, resolve violation | No update       |
| 5     | 'b'        | $b\colon 0\to 1$           | 0               | $[2..5] = \text{"aacb"}$| Set $L^*=4$     |

The returned substring is $T[2..5]=\text{"aacb"}$ of length $4$, conforming to the Parikh budget $\mathrm{sup}(a)=2, \mathrm{sup}(b)=1$.

## 5. Specializations and Connections

- **Single-Symbol Cases**: For $P$ with a single symbol $x$ at frequency $m$, MFSP reduces to the classic search for the longest run of $x$ in $T$.
- **Alphabet Reduction**: If $\sigma$ is large but $|\mathrm{supp}(P) \cup \mathrm{supp}(T)|$ is small, the alphabet can be compressed to a smaller effective size $\sigma' \ll \sigma$ prior to algorithm execution.
- **Permutation Matching and Packing**: MFSP generalizes permutation matching, which asks whether some substring's Parikh vector exactly matches $P$; this variant is handled by maintaining a difference vector and can also be solved in $O(n+\sigma)$ time within this framework.
- **Non-overlapping Occurrences**: After enumerating all length-$m$ matches, maximizing the count of disjoint occurrences reduces to the equal-length interval packing problem, solved by left-to-right greedy selection in $O(n)$ time.

## 6. Generalizations and Further Directions

- **Approximate and Weighted Variants**: MFSP admits relaxation to allow limited violation (e.g., $\sum_c \max\{0, \mathrm{cnt}[c] - \mathrm{sup}[c]\} \leq k$), or extension to weighted settings where each symbol is associated with a cost or value, yielding knapsack-like, resource-constrained substring optimization problems.
- **Relation to Sliding-Window and Packing Paradigms**: MFSP demonstrates that frequency-based string matching admits precise resource-budget interpretations, connecting classic sliding-window substring search with packing-style optimization. This suggests a broader applicability in text algorithms and resource allocation frameworks [2601.09577].

## 7. Summary

MFSP formalizes and solves, in optimal $O(n+\sigma)$ time and $O(\sigma)$ space, the problem of finding the longest substring under Parikh-component supply constraints. This sliding-window, two-pointer algorithm extends permutation matching, supports concise maximal-resource interpretations, and links string algorithms to combinatorial packing primitives [2601.09577].

Source: https://www.emergentmind.com/topics/maximum-feasible-substring-under-pattern-supply-mfsp