"""rwr² — Translate k-OA to k-ORE (Algorithm 3, arXiv 1004.2372).

rwr²(G):
  1: compute a marking H of G
  2: return strip(rwr²₁(H))
"""

import re
from .marking import mark_koa
from .rwr0 import rwr0


def strip(expr):
    """Remove k-ORE markers: a_i → a."""
    return re.sub(r'_\d+', '', expr)


def rwr_sq(G):
    """
    |———— Algorithm 3: rwr² ————|
    Require: k-OA G
    Ensure: k-ORE r with L(G) ⊆ L(r)

    1: H ← marking of G
    2: return strip(rwr²₁(H))
    """
    H = mark_koa(G)
    result = rwr0(H)
    if result is None or result == '∅':
        return None
    return strip(result)
