- Related Questions & Answers
- Check if frequency of all characters can become same by one removal in Python
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- Check if the frequency of all the digits in a number is same in Python
- Python program to check if a string contains all unique characters
- C# program to determine if a string has all unique characters
- How to check if a string has at least one letter and one number in Python?
- Check if array contains contiguous integers with duplicates allowed in Python
- Python - Check if frequencies of all characters of a string are different
- Check if frequency of characters are in Recaman Series in Python
- Python - Check if all elements in a List are same
- Check if both halves of the string have same set of characters in Python
- Count all prefixes in given string with greatest frequency using Python
- How to determine if the string has all unique characters using C#?
- Check if a binary string has two consecutive occurrences of one everywhere in C++
- Check if characters of one string can be swapped to form other in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

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))

"xyyzx"

True

Advertisements