String Letter Difference Calculator

  • Share this:

Code introduction


Calculate the letter differences between two strings, returning the common letters and their counts in both strings, as well as the difference counts.


Technology Stack : Built-in libraries

Code Type : String processing

Code Difficulty : Intermediate


                
                    
def aordiff(s1, s2):
    def get_alpha_diff(s):
        return {char: s.count(char) for char in set(s)}

    diff1 = get_alpha_diff(s1)
    diff2 = get_alpha_diff(s2)

    common_chars = set(diff1.keys()) & set(diff2.keys())
    common_counts = {char: min(diff1[char], diff2[char]) for char in common_chars}
    diff_counts = {char: diff1[char] - diff2[char] for char in common_chars}

    return common_counts, diff_counts