Soluție HackerRank pentru Making Anagrams. Include cerința formatată, exemple, explicația pașilor și cod sursă.
- Problemă: Making Anagrams
Cerinta completa
We consider two strings to be anagrams of each other if the first string’s letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency. For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.
Alice is taking a cryptography class and finding anagrams to be very useful. She decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?
Given two strings, [Expresie matematică indisponibilă în copia arhivată] and [Expresie matematică indisponibilă în copia arhivată], that may not be of the same length, determine the minimum number of character deletions required to make [Expresie matematică indisponibilă în copia arhivată] and [Expresie matematică indisponibilă în copia arhivată] anagrams. Any characters can be deleted from either of the strings.
Example.
[Expresie matematică indisponibilă în copia arhivată]
[Expresie matematică indisponibilă în copia arhivată]
The only characters that match are the [Expresie matematică indisponibilă în copia arhivată]‘s so we have to remove [Expresie matematică indisponibilă în copia arhivată] from [Expresie matematică indisponibilă în copia arhivată] and [Expresie matematică indisponibilă în copia arhivată] from [Expresie matematică indisponibilă în copia arhivată] for a total of [Expresie matematică indisponibilă în copia arhivată] deletions.
Function Description
Complete the makingAnagrams function in the editor below.
makingAnagrams has the following parameter(s):
- string s1: a string
- string s2: a string
Returns
- int: the minimum number of deletions needed
Input Format
The first line contains a single string, [Expresie matematică indisponibilă în copia arhivată].
The second line contains a single string, [Expresie matematică indisponibilă în copia arhivată].
Constraints
- [Expresie matematică indisponibilă în copia arhivată]
- It is guaranteed that [Expresie matematică indisponibilă în copia arhivată] and [Expresie matematică indisponibilă în copia arhivată] consist of lowercase English letters, ascii[a-z].
Sample Input
cde
abc
Sample Output
4
Explanation
Delete the following characters from our two strings to turn them into anagrams:
- Remove
dandefromcdeto getc. - Remove
aandbfromabcto getc.
[Expresie matematică indisponibilă în copia arhivată] characters have to be deleted to make both strings anagrams.
Limbajul de programare folosit: python3
Cod:
#!/bin/python3
from collections import Counter
def makingAnagrams(s1, s2):
c1 = Counter(s1)
c2 = Counter(s2)
keys = set(c1) | set(c2)
return sum(abs(c1.get(k, 0) - c2.get(k, 0)) for k in keys)
if __name__ == '__main__':
s1 = input().strip()
s2 = input().strip()
print(makingAnagrams(s1, s2))
Scor obtinut: 1.0
Submission ID: 464591133
Link challenge: https://www.hackerrank.com/challenges/making-anagrams/problem
