Guys can someone help me out as to why this logic fails
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3382 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 155 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 145 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 134 |
| 9 | maroonrk | 132 |
| 10 | cry | 130 |
| Name |
|---|



Here's what I found wrong with this code as someone who has solved this problem:
1: Initialization of mini = INT_MAX and checking of "if(mini != n)". This should be "if(mini != INT_MAX)" since mini is almost never equal to n, resulting in printing INT_MAX or something else instead of the intended -1 when you don't find a valid substring.
2: Code runs in O(n^2) with worst-case scenarios because you track all of acount and bcount in a set and bruteforce j values, and you could do it in O(n) with prefix sums and a hash-map storing earliest prefix index.
Hope this helps!
Won't mini always become n in the end because a possible soln is to always remove the entire elements of the string
Also I did figure out where my code was going wrong. I was checking and updating mini only when c[x]-c[y]>=delta but right answer can exist for any c[x] and c[y] and you're right it's an N^2 soln but like I just wanted to understand that for which input my logic giving wrong answer. Thanks for helping out man!
The reason mini won’t always become n in the end is because the problem explicitly asks to print -1 when the only way to equalize counts is to remove the entire string (“If it is necessary to remove all letters from the string s (i.e., make it empty), report this”). So if your solution treats a removal of length n as valid instead of printing -1, it would be considered incorrect according to the problem statement.
isn't this doing that then, and also man I feel super dumb atp you're solving 3000+ rated questions and I'm asking beginner doubts btw I did solve this question now using the hash map and prefix approach thanks again <3.
The problem with if(mini != n) check is that it only works if the only invalid solution is removing the entire string, but in the original code, mini could never be set to n in most cases, like if no valid substring existed. As a result, you'd either skip printing -1, or print a wrong value such as INT_MAX, which is why the correct check should be if(mini != INT_MAX && mini != n).
Glad I could help you solve it :)
Sorry to say, but the code looks a bit messy (for me -_- ). From what I understand, you’re comparing the delta of prefix and suffix, and I think your logic and mine are actually the same — just implemented differently. You can follow this code : 342354714, Ig.