Original Problem:
A word was K-good if for every two letters in the word, if the first appears x
times and the second appears y
times, then |x - y| ≤ K.
Given some word w, how many letters does he have to remove to make it K-good?
Problem Link.
I have solved the above problem and i not asking solution for the above problem
I just misread the statement for first time and just thought how can we solve this problem in linear line time , which just give rise to a new problem
Modification Problem
A word was K-good if for every two consecutive letters in the word, if the first appears x
times and the second appears y
times, then |x - y| ≤ K.
Given some word w, how many letters does he have to remove to make it K-good?
Is this problem is solvable in linear time , i thought about it but could not find any valid solution.
Solution
My Approach: I could not approach my crush but her is my approach to this problem , try everything( from movie Zooptopia)
i.e.
for i range(0,1<<n): // n length of string for j in range(0,n): if(i&(1<<j) is not zero): delete the character Now check if String is K good
For N
in Range 10^5
. Time Complexity: Time does not exist in that dimension.
Is there any linear solution to this problem , simple and sweet like people of stackoverflow.
For Ex: String S = AABCBBCB and K=1 If we delete 'B' at index 5 so String S = AABCBCB which is good string F[A]-F[A]=0 F[B]-F[A]=1 F[C]-F[B]=1 and so on
I guess this is a simple example there can me more complex example as deleting an I element makens (I-1) and (I+1) as consecutive
2 Answers
Answers 1
Is there any linear solution to this problem?
Consider the word DDDAAABBDC
. This word is 3-good, becauseD
andC
are consecutive and card(D)-card(C)=3
, and removing the lastD
makes it 1-good by makingD
andC
non-consecutive.
Inversely if I consider DABABABBDC
which is 2-good, removing the lastD
makes C
andB
consecutive and increases the K-value of the word to 3.
This means that in the modified problem, the K-value of a word is determined by both the cardinals of each letter and the cardinals of each couple of consecutive letters.
By removing a letter, I reduce its cardinal of the letter as well as the cardinals of the pairs to which it belongs, but I also increase the cardinal of other pair (potentially creating new ones).
It is also important to notice that if in the original problem, all letters are equivalent (I can remove any indifferently), while it is no longer the case in the modified problem.
As a conclusion, I think we can safely assume that the "consecutive letters" constrain makes the problem not solvable in linear time for any alphabet/word.
Answers 2
Let's say string length is
N
and we wanted to have a solution for N = 10^5.
One approach is to make a interval of allowed frequency of character. O(26N) or O(N)
i.e. Linear with respect to string length.
- You can make an array counting the frequency of different character.
- Generate intervals
lower_bound
toupper_bound
, such thatupper_bound = lower_bound + K
. Simply by iteratinglower_bound
from 1 toMAX_FREQUENCY
and makingupper_bound=lower_bound + K
- For every character(a-z), calculate how many character to remove to make frequency comes within
(lower_bound,upper_bound)
or become equal to0
. - Sum up, required number of removal for this interval, let's say
S
.
- For every character(a-z), calculate how many character to remove to make frequency comes within
- Find the minimum of
S
.
0 comments:
Post a Comment