I am trying to solve this question from leetcode…
The question is 809. Expressive Words
Even i watched the solution, i am not able to understand the logic.
Could you help me please to understand the solution?
Thanks
Not able to understand the solution
post the solution code here
I will explain it step by step
This code is in java… Mentioned in solution…
In this question, given a string S and a group of other words(queries), you have to find out how many queries match with S if the stretch operation is applied.
S = “heeellooo”
words = [“hello”, “hi”, “helo”]
Output: 1
Explanation: We can extend “e” and “o” in the word “hello” to get “heeellooo”. We can’t extend “helo” to get “heeellooo” because the group “ll” is not size 3 or more.
Now the solution uses 2 things : the root of the word and the frequency of letters
- The root denotes the alphabets used in the word. As example for hello the root would be [h,e,l,o].
If a query has to be same as S then it is evident that both the query and S should have same root. So this is the first check condition.
This root has been found out of each word using RLE function in the code. - The second thing is the frequency of each alphabet present inside the word. Let the query’s ith alphabet frequency be c2 and S’s ith frequency alphabet be c1.
Now 3 cases arise from here.
a.) c1 > c2 : here the frequency is more in the query than S. Since we can only add frequency in the stretch operation hence it would be impossible to reduce c1 to match c2.
b.) c1 >= 3 then we can increase the frequency for query’s ith character using stretch operation to match that of S.
c.) c1 < 3 then c1 == c2 is the only possible case as stretch only allows increment for 3 or more.
These conditions have been checked inside the loop after checking condition 1.
I have tried to come up with the code… for that logic…
But it is showing wrong answer for some test case…
Not able to find the bug…
Could you see this please
It got accepetd… Little mistake i was doing…
Thank you very much…
please code the doubt then.
Happy to help!!!