def weighted_jaccard_similarity(A, B):
    """
    Function computing the weighted Jaccard similarity.

    Runs in O(n), n being the sum of A & B's sizes.

    Args:
        A (Counter): First weighted set.
        B (Counter): Second weighted set.

    Returns:
        float: Weighted Jaccard similarity between A & B.

    """
    if A is B:
        return 1.0
    if len(A) == 0 or len(B) == 0:
        return 0.0
    I = 0.0
    U = 0.0
    done = set()
    if len(A) > len(B):
        A, B = B, A
    for k, weight_A in A.items():
        weight_B = B.get(k, 0.0)
        if weight_B != 0.0:
            done.add(k)
        if weight_A < weight_B:
            I += weight_A
            U += weight_B
        else:
            I += weight_B
            U += weight_A
    for k, v in B.items():
        if k in done:
            continue
        U += v
    return I / U
