- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using Counter() in Python 3.x. to find minimum character removal to make two strings anagram
In this article, we will learn about how we can make a string into a pangram using the counter() function in Python 3.x. Or earlier. To do so we are allowed to remove any character from the input string. We will also find the number of such required characters to be removed to make the string into an anagram.
Two strings are said to be anagrams of each other when they contain the same type of alphabets in any random order.
The counter () method is present in the collection module available in Python. The prerequisite is to import the collections module to use the counter() function.
1. Conversion of input string into a dictionary type having characters as keys and their frequency as values using Counter(inp_str) available in the collections module. 2. Counting the total number of keys and counting the number of keys in common to both dictionaries converted from input strings. 3. If no common keys are detected this means there is a need for removal of (sum of the length of both dictionaries) characters from both the input strings. 4. otherwise (max(length of both dictionaries) – number of Common keys available ) will give the required number of characters to be removed
collections.Counter is a dictionary subclass to ensure automatic counting the letters by the interpreter. We don’t actually need to create the substrings or check whether they are anagrams manually.
# two strings become anagram from collections import Counter def convertAnagram(str_1, str_2): # conversion of strings to dictionary type dict_1 = Counter(str_1) dict_2 = Counter(str_2) keys_1 = dict_1.keys() keys_2 = dict_2.keys() # count number of keys in both lists of keys count_1 = len(keys_1) count_2 = len(keys_2) # convert pair of keys into set to find common keys set_1 = set(keys_1) commonKeys = len(set_1.intersection(keys_2)) if (commonKeys == 0): # no common things found i.e. all are distinct return (count_1 + count_2) else: # some elements are already matching in input return (max(count_1, count_2)-commonKeys) str_1 ='Tutorials' str_2 ='sTutalori' str_3='Point' print (convertAnagram(str_1, str_2)) print (convertAnagram(str_3, str_2))
In this article, we learnt how we can make two string anagram of one another by counting the number of characters needed for maintaining the anagram relationship Python 2.x. needed.
- Minimum Number of Steps to Make Two Strings Anagram in C++
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character in C++
- Program to find minimum deletions to make strings strings in Python
- Program to find minimum swaps required to make given anagram in python
- Minimum removal to make palindrome permutation in C++
- Minimum Cost To Make Two Strings Identical in C++
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Program to split two strings to make palindrome using Python
- Minimum Cost to make two Numeric Strings Identical in C++
- Minimum number of given operations required to make two strings equal using C++.
- Java Program to Check if two strings are anagram
- Program to find minimum operations to make array equal using Python
- Program to partition two strings such that each partition forms anagram in Python
- Program to find minimum operations needed to make two arrays sum equal in Python
- Minimum Swaps to Make Strings Equal in C++