{"id":18853,"date":"2021-08-26T10:37:51","date_gmt":"2021-08-26T05:07:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=18853"},"modified":"2021-11-22T18:37:19","modified_gmt":"2021-11-22T13:07:19","slug":"python-program-to-check-if-all-characters-have-even-frequency","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-all-characters-have-even-frequency\/","title":{"rendered":"Python Program to Check if All Characters have Even Frequency"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Two Odd Occurring Elements in an Array\/List<\/a> Counter function in Python:<\/strong><\/p>\n The counter is a set and dict subset. Counter() takes an iterable entity as an argument and stores the elements as keys and the frequency of the elements as a value. So, in collections, if we transfer a string. When you call Counter(), you\u2019ll get a Counter class object with characters as keys and their frequency in a string as values.<\/p>\n Counter() returns a Counter type object (a subclass of dict) with all characters in the string as keys and their occurrence count as values. We\u2019ll use the [] operator to get the occurrence count of the characters from it.<\/p>\n For example:<\/p>\n let given string = “pqrspqrspp”.<\/p>\n In this p<\/strong> occurred 4 times, q,r,s<\/strong> occurred 2 times.<\/p>\n Therefore the frequency of all the characters in a given string is Even.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check whether all characters in the given string made up entirely of lowercase letters have an even frequency:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Find Two Odd Occurring Elements in an Array\/List The task is to check whether all characters in the given string made up entirely of lowercase letters have an even frequency. Counter function in Python: The counter is a set and dict subset. Counter() takes an …<\/p>\n
\nThe task is to check whether all characters in the given string made up entirely of lowercase letters have an even frequency.<\/p>\nGiven String = \"pqrspqrstutu\"<\/pre>\n
Yes,the given string { pqrspqrstutu } contains all characters at even intervals<\/pre>\n
Given String = \"btechgeeks\"<\/pre>\n
No,the given string { btechgeeks } does not contains all characters at even intervals<\/pre>\n
Program to Check if All Characters have Even Frequency in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Pass the given string as an argument to the function determine.\r\n\r\n\r\ndef determine(gvn_strng):\r\n # Calculate the frequency of all the given string elements using the Counter() function\r\n # which returns the element and its frequency as a key-value pair and stores this\r\n # dictionary in another variable (say strngfreqelements).\r\n\r\n strngfreqelements = Counter(gvn_strng)\r\n # Traverse in this frequency dictionary using the for loop.\r\n for key in strngfreqelements:\r\n # Inside the loop, check if the Key has a value even or odd using the modulus operator\r\n # and if conditional statement.\r\n if (strngfreqelements[key] % 2 == 1):\r\n # If the statement is true, then return False.\r\n return False\r\n # Return true after the for loop.\r\n return True\r\n\r\n\r\n# Give the string as static input and store it in a variable.\r\ngvn_strng = \"pqrspqrstutu\"\r\n# Check if the function returns true or false using the if conditional statement.\r\nif(determine(gvn_strng)):\r\n # If it is true, print \"yes, the given string contains all characters at even intervals\".\r\n print(\"Yes,the given string {\", gvn_strng,\r\n \"} contains all characters at even intervals\")\r\nelse:\r\n # Else print \"No, the given string does not contain all characters at even intervals\".\r\n print(\"No,the given string {\", gvn_strng,\r\n \"} does not contains all characters at even intervals\")\r\n<\/pre>\n
Yes,the given string { pqrspqrstutu } contains all characters at even intervals<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Pass the given string as an argument to the function determine.\r\n\r\n\r\ndef determine(gvn_strng):\r\n # Calculate the frequency of all the given string elements using the Counter() function\r\n # which returns the element and its frequency as a key-value pair and stores this\r\n # dictionary in another variable (say strngfreqelements).\r\n\r\n strngfreqelements = Counter(gvn_strng)\r\n # Traverse in this frequency dictionary using the for loop.\r\n for key in strngfreqelements:\r\n # Inside the loop, check if the Key has a value even or odd using the modulus operator\r\n # and if conditional statement.\r\n if (strngfreqelements[key] % 2 == 1):\r\n # If the statement is true, then return False.\r\n return False\r\n # Return true after the for loop.\r\n return True\r\n\r\n\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvn_strng = input(\"Enter some random String = \")\r\n# Check if the function returns true or false using the if conditional statement.\r\nif(determine(gvn_strng)):\r\n # If it is true, print \"yes, the given string contains all characters at even intervals\".\r\n print(\"Yes,the given string {\", gvn_strng,\r\n \"} contains all characters at even intervals\")\r\nelse:\r\n # Else print \"No, the given string does not contain all characters at even intervals\".\r\n print(\"No,the given string {\", gvn_strng,\r\n \"} does not contains all characters at even intervals\")\r\n<\/pre>\n
Enter some random String = btechgeeks\r\nNo,the given string { btechgeeks } does not contains all characters at even intervals<\/pre>\n
\n