[step:Reduce the problem to partitioning $A$ and $B$ compatibly under $f$ and $g$]Suppose we can find a subset $P \subseteq A$ with the following property: writing $Q := A \setminus P$, $R := f(P) \subseteq B$, and $S := B \setminus R$, we have
\begin{align*}
g(S) = Q.
\end{align*}
Define
\begin{align*}
h: A &\to B, \\
h(a) &= \begin{cases} f(a) & \text{if } a \in P, \\ g^{-1}(a) & \text{if } a \in Q. \end{cases}
\end{align*}
We verify $h$ is well-defined and bijective.
**Well-defined.** If $a \in P$, then $f(a) \in B$ is defined because $f: A \to B$. If $a \in Q = g(S)$, then $a = g(b)$ for some $b \in S \subseteq B$; since $g$ is injective, this $b$ is unique, so $g^{-1}(a)$ is unambiguously defined.
**Injectivity.** Suppose $h(a_1) = h(a_2)$. If $a_1, a_2 \in P$, then $f(a_1) = f(a_2)$, so $a_1 = a_2$ by injectivity of $f$. If $a_1, a_2 \in Q$, then $g^{-1}(a_1) = g^{-1}(a_2)$; applying $g$ gives $a_1 = a_2$. Finally, if $a_1 \in P$ and $a_2 \in Q$, then $h(a_1) = f(a_1) \in R$ and $h(a_2) = g^{-1}(a_2) \in S$; but $R \cap S = \varnothing$, so $h(a_1) \neq h(a_2)$, contradicting the assumption. Hence injectivity holds.
**Surjectivity.** Let $b \in B$. Either $b \in R = f(P)$, in which case $b = f(a) = h(a)$ for some $a \in P$; or $b \in S$, in which case $g(b) \in g(S) = Q$, so $h(g(b)) = g^{-1}(g(b)) = b$. Hence $h$ is surjective.
Thus $h: A \to B$ is a bijection, and the theorem reduces to producing $P \subseteq A$ with $g(B \setminus f(P)) = A \setminus P$.[/step]