Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Maximum Frequency Character in String - Python
Next article icon

Python program to equal character frequencies

Last Updated : 16 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a String, ensure it has equal character frequencies, if not, equate by adding required characters.

Input : test_str = ‘geeksforgeeks’ 
Output : geeksforgeeksggkkssfffooorrr 
Explanation : Maximum characters are 4 of ‘e’. Other character are appended of frequency 4 – (count of chars).

Input : test_str = ‘geksforgeeks’ 
Output : geeksforgeeksggksffoorr 
Explanation : Maximum characters are 3 of ‘e’. Other character are appended of frequency 3 – (count of chars).  

Method #1 : Using count() + max() + loop

In this, we get the frequency of the maximum occurring character, and then append each character to match the maximum character frequency.

Python3




# Python3 code to demonstrate working of
# Equal character frequencies
# Using max() + count() + loop
 
# initializing string
test_str = 'geeksforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# getting maximum frequency character
max_freq = max([test_str.count(ele) for ele in test_str])
 
# equating frequencies
res = test_str
for chr in test_str:
     
    # if frequencies don't match max_freq
    if res.count(chr) != max_freq:
        res += chr * (max_freq - test_str.count(chr))
     
# printing result
print("Equal character frequency String : " + str(res))
 
 
Output
The original string is : geeksforgeeks Equal character frequency String : geeksforgeeksggkkssfffooorrr 

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using Counter() + loop 

Similar to the above, the difference being Counter() is used to get the maximum element count.

Python3




# Python3 code to demonstrate working of
# Equal character frequencies
# Using Counter() + loop
from collections import Counter
 
# initializing string
test_str = 'geeksforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# getting maximum frequency character
# using Counter()
freq_dict = Counter(test_str)
max_freq = test_str.count(max(freq_dict, key = freq_dict.get))
 
# equating frequencies
res = test_str
for chr in test_str:
     
    # if frequencies don't match max_freq
    if res.count(chr) != max_freq:
        res += chr * (max_freq - test_str.count(chr))
     
# printing result
print("Equal character frequency String : " + str(res))
 
 
Output
The original string is : geeksforgeeks Equal character frequency String : geeksforgeeksggkkssfffooorrr 

Time Complexity: O(n)
Auxiliary Space: O(n)



Next Article
Maximum Frequency Character in String - Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • Python Program to calculate Dictionaries frequencies
    Given a list of dictionaries, the task here is to write a python program to extract dictionary frequencies of each dictionary. Input : test_list = [{'gfg' : 1, 'is' : 4, 'best' : 9}, {'gfg' : 6, 'is' : 3, 'best' : 8}, {'gfg' : 1, 'is' : 4, 'best' : 9}, {'gfg' : 1, 'is' : 1, 'best' : 9}, {'gfg' : 6,
    6 min read
  • Python - Expand Character Frequency String
    Given a string, which characters followed by its frequency, create the appropriate string. Examples: Input : test_str = 'g7f2g3i2s2b3e4' Output : gggggggffgggiissbbbeeee Explanation : g is succeeded by 7 and repeated 7 times. Input : test_str = 'g1f1g1' Output : gfg Explanation : f is succeeded by 1
    4 min read
  • Odd Frequency Characters - Python
    The task of finding characters with odd frequencies in a string in Python involves identifying which characters appear an odd number of times. For example, in the string "geekforgeeks," characters like 'r', 'o', 'f', and 's' appear an odd number of times. The output will be a list of these character
    3 min read
  • Maximum Frequency Character in String - Python
    The task of finding the maximum frequency character in a string involves identifying the character that appears the most number of times. For example, in the string "hello world", the character 'l' appears the most frequently (3 times). Using collection.CounterCounter class from the collections modu
    3 min read
  • Python program to find occurrence to each character in given string
    Given a string, the task is to write a program in Python that prints the number of occurrences of each character in a string. There are multiple ways in Python, we can do this task. Let's discuss a few of them. Method #1: Using set() + count() Iterate over the set converted string and get the count
    5 min read
  • Python - Least Frequent Character in String
    The task is to find the least frequent character in a string, we count how many times each character appears and pick the one with the lowest count. Using collections.CounterThe most efficient way to do this is by using collections.Counter which counts character frequencies in one go and makes it ea
    3 min read
  • Consecutive characters frequency - Python
    This problem involves identifying characters that appear consecutively and counting how many times they appear together. Here, we will explore different methods to calculate the frequency of consecutive characters in a string. Using regular expressionsWe can use the re module to efficiently count co
    3 min read
  • Python | Construct string from character frequency tuple
    Sometimes, while working with data, we can have a problem in which we need to perform construction of string in a way that we have a list of tuple having character and it's corresponding frequency and we require to construct a new string from that. Let's discuss certain ways in which this task can b
    5 min read
  • Python Program to find if a character is vowel or Consonant
    Given a character, check if it is vowel or consonant. Vowels are 'a', 'e', 'i', 'o' and 'u'. All other characters ('b', 'c', 'd', 'f' ....) are consonants. Examples: Input : x = 'c'Output : ConsonantInput : x = 'u'Output : VowelRecommended: Please try your approach on {IDE} first, before moving on t
    5 min read
  • Python program to read character by character from a file
    Python is a great language for file handling, and it provides built-in functions to make reading files easy with which we can read file character by character. In this article, we will cover a few examples of it. Example Input: GeeksOutput: G e e k sExplanation: Iterated through character by charact
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences