from typing import Tuple
import math


def near_duplicate_similarity_cached(
        gold: Tuple[str],
        pred: Tuple[str],
        threshold: float = 0.1,
) -> float:
    """
    Computes the approximate token-level accuracy between gold and pred.

    Returns:
        token-level accuracy - if not exact match and mismatching tokens <= threshold;
        0 - otherwise.
    """
    mismatch_allowed = int(math.ceil(threshold * min(len(gold), len(pred))))

    # Check length difference
    if abs(len(gold) - len(pred)) >= mismatch_allowed:
        return 0

    # Count number of mismatches
    mismatch_count = 0
    max_len = max(len(gold), len(pred))
    for i in range(max_len):
        if i >= len(gold):
            mismatch_count += len(pred) - i
            break
        if i >= len(pred):
            mismatch_count += len(gold) - i
            break
        if gold[i] != pred[i]:
            mismatch_count += 1
            if mismatch_count >= mismatch_allowed:
                return 0
    if mismatch_count >= mismatch_allowed:
        return 0
    else:
        return 1 - mismatch_count / max_len