
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if a string has all characters with same frequency with one variation allowed in Python
Suppose we have a lowercase string s, we have to check whether we can convert s into a valid string by deleting at most 1 character. Here a valid string means a string str such that for all unique characters in str each character’s frequency is same.
So, if the input is like s = "xyyzx", then the output will be True as we can delete z then string will be "xyyx" where occurrences of x and y are same.
To solve this, we will follow these steps −
- size := 26
- occurrence := an array of size 26. This is storing frequencies of each character in s
- occr1 := 0
- occr1_cnt := 0
- for i in range 0 to size - 1, do
- if occurrence[i] is not 0, then
- occr1 := occurrence[i]
- occr1_cnt := 1
- come out from loop
- if occurrence[i] is not 0, then
- occr2 := 0
- occr2_cnt := 0
- for j in range i+1 to size - 1, do
- if occurrence[j] is not 0, then
- if occurrence[j] is same as occr1, then
- occr1_cnt := occr1_cnt + 1
- otherwise,
- occr2_cnt := 1
- occr := occurrence[j]
- come out from loop
- if occurrence[j] is same as occr1, then
- if occurrence[j] is not 0, then
- for k in range j+1 to size - 1, do
- if occurrence[k] is not 0, then
- if occurrence[k] is same as occr1, then
- occr1_cnt := occr1_cnt + 1
- if occurrence[k] is same as occr2, then
- occr2_cnt := occr2_cnt + 1
- otherwise,
- return False
- if occurrence[k] is same as occr1, then
- if occr1_cnt > 1 and occr2_cnt > 1, then
- return False
- if occurrence[k] is not 0, then
- return True
Let us see the following implementation to get better understanding −
Example
size = 26 def solve(str): occurrence = [0]*size for i in range(len(str)): occurrence[ord(str[i])-ord('a')] += 1 occr1 = 0 occr1_cnt = 0 for i in range(size): if (occurrence[i] != 0): occr1 = occurrence[i] occr1_cnt = 1 break occr2 = 0 occr2_cnt = 0 for j in range(i+1,size): if (occurrence[j] != 0): if (occurrence[j] == occr1): occr1_cnt += 1 else: occr2_cnt = 1 occr = occurrence[j] break for k in range(j+1,size): if occurrence[k] != 0: if (occurrence[k] == occr1): occr1_cnt += 1 if (occurrence[k] == occr2): occr2_cnt += 1 else: return False if occr1_cnt > 1 and occr2_cnt > 1: return False return True s = "xyyzx" print(solve(s))
Input
"xyyzx"
Output
True
- Related Articles
- Check if frequency of all characters can become same by one removal in Python
- Python - Check If All the Characters in a String Are Alphanumeric?
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- Python program to check if a string contains all unique characters
- Python - Check if frequencies of all characters of a string are different
- Check if array contains contiguous integers with duplicates allowed in Python
- Check if the frequency of all the digits in a number is same in Python
- Check if both halves of the string have same set of characters in Python
- Check if frequency of characters are in Recaman Series in Python
- C# program to determine if a string has all unique characters
- Count all prefixes in given string with greatest frequency using Python
- How to check if a string has at least one letter and one number in Python?
- Check if both halves of the string have the same set of characters in Python
- Check if characters of one string can be swapped to form other in Python
- Python - Check if all elements in a List are same

Advertisements