def check_permutation2(u, v):
    """
    Second solution walks through both u and v and builds a dict
    of distinct characters and their counts for each. 
    It then compares the counts of characters between them.
    Runs in O(max(n, m)) where n = len(u) and m = len(v).
    """
    u_chars = {}
    for c in u:
        try:
            u_chars[c] += 1
        except KeyError:
            u_chars[c] = 1

    v_chars = {}
    for d in v:
        try:
            v_chars[d] += 1
        except KeyError:
            v_chars[d] = 1

    if sum(u_chars.values()) != sum(v_chars.values()):
        #u and v are not of the same length.
        return False

    for c in u:
        c_count_in_u = u_chars[c]
        c_count_in_v = v_chars.get(c, 0)
        if c_count_in_u != c_count_in_v:
            return False

    return True