Last active
August 11, 2018 07:11
-
-
Save anuj9196/b0739859c249ce297e8f7b2c3c6eb0ff to your computer and use it in GitHub Desktop.
Program to count number of items to be deleted to make two strings anagrams.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Given two strings, a and b, that may or may not be of same length, | |
determine the minimum number of character deletions required to make a and b anagrams. | |
Any characters can be deleted from either of the strings | |
NOTE: Anagram of a word is formed by arranging the letters of the word. | |
For eg. -> for the word RAM: RAM, AMR, MAR, etc are the anagrams | |
""" | |
attempts = int(input()) | |
for x in range(attempts): | |
str1 = input('Enter 1st string: ') | |
str2 = input('Enter 2nd string: ') | |
num_remove_str1 = num_remove_str2 = 0 | |
for l in set(str1): | |
if l not in str2: | |
num_remove_str1 += str1.count(l) | |
continue | |
if str1.count(l) > str2.count(l): | |
num_remove_str1 += str1.count(l) - str2.count(l) | |
if str1.count(l) < str2.count(l): | |
num_remove_str2 += str2.count(l) - str1.count(l) | |
for l in set(str2): | |
if l not in str1: | |
num_remove_str2 += str2.count(l) | |
print('num of letter to remove from "' + str1 + '": ' + str(num_remove_str1)) | |
print('num of letter to remove from "' + str2 + '": ' + str(num_remove_str2)) | |
print('num of minimum letters to remove: ' + str(num_remove_str1+num_remove_str2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment