I am trying to compare the two strings: 'apple' and 'pear' and return letters that do not belong to the other string.
For example, 'apple' does not contain 'r' in 'pear'
'pear' does not contain 'l' and 'p' in apple (pear contains p but does not contains two p's).
So I want to have a function that returns 'r', 'l', and 'p'.
I tried set, but it ignores the duplicates (p, in this example).
def solution(A, B):
N = len(A)
M = len(B)
letters_not_in_B = list(set([c for c in A if c not in B]))
letters_not_in_A = list(set([c for c in B if c not in A]))
answer = len(letters_not_in_B) + len(letters_not_in_A)
return answer
You can compare the character counts for each separate string resulting from the concatenation of the parameters a and b:
def get_results(a, b):
return list(set([i for i in a+b if a.count(i) != b.count(i)]))
print(get_results('apple', 'pear'))
Output:
['p', 'r', 'l']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With