In the previous article, we have discussed Python Program to Remove Characters that Appear More than k Times
Given a string, K value and the task is to remove all the characters from the given string that appears strictly less than k times.
Examples:
Example1:
Input:
Given String = "hellobtechgeekssss " Given k value = 2
Output:
The given string { hellobtechgeekssss } after removal of all characters that appears more than k{ 2 } times : helleheessss
Example2:
Input:
Given String = "gooodmorningallll " Given k value= 3
Output:
The given string { gooodmorningallll } after removal of all characters that appears more than k{ 3 } times : oooollll
Program to Remove Elements that Appear Strictly Less than k Times in Python
Below are the ways to remove all the characters from the given string that appears strictly less than k times:
- Using Dictionary (Hashing, User Input)
- Using Counter() function (Hashing, Static Input)
- Using Counter() function (Hashing, User Input)
- Using Counter() function (Hashing, User Input)
Method #1: Using Dictionary (Hashing, Static Input)
Approach:
- Take a dictionary and initialize it to empty using the {} or dict() say freqncyDictionary.
- Give the string as static input and store it in a variable.
- Loop in the given string using the For loop.
- Inside the For loop, Check if the string character is present in the dictionary or not using the if conditional statement and ‘in’ keyword.
- If it is true then increment the count of the string character in the dictionary by 1.
- Else initialize the dictionary with the string character as key and value as 1.
- Give the k value as static input and store it in a variable.
- Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
- loop in the given string using the for loop.
- Check if the character has a frequency greater than or equal to k by checking the value of that character in the frequency dictionary.
- we check using the if conditional statement
- If it is true then concatenate this character to modifd_string using string concatenation.
- Print the modifd_string string.
- The Exit of the Program.
Below is the implementation:
# Take a dictionary and initialize it to empty # using the {} or dict() say freqncyDictionary. freqncyDictionary = {} # Give the string as static input and store it in a variable gvnstrng = "hellobtechgeekssss" # Loop in the given string using the For loop. for i in gvnstrng: # Inside the For loop, # Check if the string character is present in the dictionary # or not using the if conditional statement and 'in' keyword. if i in freqncyDictionary.keys(): # If it is true then increment the count of the string character # in the dictionary by 1. freqncyDictionary[i] = freqncyDictionary[i]+1 # Else initialize the dictionary with the string character as key and value as 1. else: freqncyDictionary[i] = 1 # Give the k value as static input and store it in a variable. k = 2 # Take a string which stores all the characters which are not occuring even number # of times and initialize it to null string using "" or str() modifd_string = "" # loop in the given string using the for loop for charac in gvnstrng: # check if the character has frequency greater than or equal to k by checking value of that character in frequency dictionary # we check using the if conditional statement if(freqncyDictionary[charac] >= k): # if it is true then concatenate this character to modifd_string using string concatenation modifd_string = modifd_string+charac # print the modifd_string string print('The given string {', gvnstrng, '} after removal of all characters that appears more than k{', k, '} times :', modifd_string)
Output:
The given string { hellobtechgeekssss } after removal of all characters that appears more than k{ 2 } times : helleheessss
Method #2: Using Dictionary (Hashing, User Input)
Approach:
- Take a dictionary and initialize it to empty using the {} or dict() say freqncyDictionary.
- Give the string as the user input using the input() function and store it in a variable.
- Loop in the given string using the For loop.
- Inside the For loop, Check if the string character is present in the dictionary or not using the if conditional statement and ‘in’ keyword.
- If it is true then increment the count of the string character in the dictionary by 1.
- Else initialize the dictionary with the string character as key and value as 1.
- Give the k value as user input using the int(input()) function and store it in a variable.
- Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
- loop in the given string using the for loop.
- Check if the character has a frequency greater than or equal to k by checking the value of that character in the frequency dictionary.
- we check using the if conditional statement
- If it is true then concatenate this character to modifd_string using string concatenation.
- Print the modifd_string string.
- The Exit of the Program.
Below is the implementation:
# Take a dictionary and initialize it to empty # using the {} or dict() say freqncyDictionary. freqncyDictionary = {} # Give the string as the user input using the input() function and store it in a variable. gvnstrng = input("Enter some random string = ") # Loop in the given string using the For loop. for i in gvnstrng: # Inside the For loop, # Check if the string character is present in the dictionary # or not using the if conditional statement and 'in' keyword. if i in freqncyDictionary.keys(): # If it is true then increment the count of the string character # in the dictionary by 1. freqncyDictionary[i] = freqncyDictionary[i]+1 # Else initialize the dictionary with the string character as key and value as 1. else: freqncyDictionary[i] = 1 # Give the k value as user input using the int(input()) function and store it in a variable. k = int(input("Enter some random number =")) # Take a string which stores all the characters which are not occuring even number # of times and initialize it to null string using "" or str() modifd_string = "" # loop in the given string using the for loop for charac in gvnstrng: # check if the character has frequency greater than or equal to k by checking value of that character in frequency dictionary # we check using the if conditional statement if(freqncyDictionary[charac] >= k): # if it is true then concatenate this character to modifd_string using string concatenation modifd_string = modifd_string+charac # print the modifd_string string print('The given string {', gvnstrng, '} after removal of all characters that appears more than k{', k, '} times :', modifd_string)
Output:
Enter some random string = gooodmorningallll Enter some random number =3 The given string { gooodmorningallll } after removal of all characters that appears more than k{ 3 } times : oooollll
Method #3: Using Counter() function (Hashing, Static Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the string as static input and store it in a variable
- Calculate the frequency of all the given string characters using the Counter() function which returns the element and its frequency as key-value pair  and store this dictionary in a variable(say freqncyDictionary)
- Give the k value as static input and store it in a variable.
- Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
- loop in the given string using the for loop.
- Check if the character has a frequency greater than or equal to k by checking the value of that character in the frequency dictionary.
- we check using the if conditional statement
- If it is true then concatenate this character to modifd_string using string concatenation.
- Print the modifd_string string.
- The Exit of the Program.
Below is the implementation:
# Import the Counter() function from collections using the import keyword. from collections import Counter # Give the string as static input and store it in a variable gvnstrng = "hellobtechgeekssss " # Calculate the frequency of all the given string characters using the Counter() # function which returns the element and its frequency as key-value pair # and store this dictionary in a variable(say freqncyDictionary) freqncyDictionary = Counter(gvnstrng) # Give the k value as static input and store it in a variable. k = 2 # Take a string which stores all the characters which are not occuring even number # of times and initialize it to null string using "" or str() modifd_string = "" # loop in the given string using the for loop for charac in gvnstrng: # check if the character has frequency greater than or equal to k by checking value of that character in frequency dictionary # we check using the if conditional statement if(freqncyDictionary[charac] >= k): # if it is true then concatenate this character to modifd_string using string concatenation modifd_string = modifd_string+charac # print the modifd_string string print('The given string {', gvnstrng, '} after removal of all characters that appears more than k{', k, '} times :', modifd_string)
Output:
The given string { hellobtechgeekssss } after removal of all characters that appears more than k{ 2 } times : helleheessss
Method #4: Using Counter() function (Hashing, User Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the string as the user input using the input() function and store it in a variable.
- Calculate the frequency of all the given string characters using the Counter() function which returns the element and its frequency as key-value pair  and store this dictionary in a variable(say freqncyDictionary)
- Give the k value as user input using the int(input()) function and store it in a variable.
- Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
- loop in the given string using the for loop.
- Check if the character has a frequency greater than or equal to k by checking the value of that character in the frequency dictionary.
- we check using the if conditional statement
- If it is true then concatenate this character to modifd_string using string concatenation.
- Print the modifd_string string.
- The Exit of the Program.
Below is the implementation:
# Import the Counter() function from collections using the import keyword. from collections import Counter # Give the string as the user input using the input() function and store it in a variable. gvnstrng = input("Enter some random string = ") # Calculate the frequency of all the given string characters using the Counter() # function which returns the element and its frequency as key-value pair # and store this dictionary in a variable(say freqncyDictionary) freqncyDictionary = Counter(gvnstrng) # Give the k value as user input using the int(input()) function and store it in a variable. k = int(input("Enter some random number =")) # Take a string which stores all the characters which are not occuring even number # of times and initialize it to null string using "" or str() modifd_string = "" # loop in the given string using the for loop for charac in gvnstrng: # check if the character has frequency greater than or equal to k by checking value of that character in frequency dictionary # we check using the if conditional statement if(freqncyDictionary[charac] >= k): # if it is true then concatenate this character to modifd_string using string concatenation modifd_string = modifd_string+charac # print the modifd_string string print('The given string {', gvnstrng, '} after removal of all characters that appears more than k{', k, '} times :', modifd_string)
Output:
Enter some random string = gooodmorningallll Enter some random number =3 The given string { gooodmorningallll } after removal of all characters that appears more than k{ 3 } times : oooollll
The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners. Using them you can develop code on your own and master coding skills.