SECPASS - Editorial

By using KMP, you can do it in O(|S|) by counting the number of times each prefix occurs in the string.
Here is the link to my code - CodeChef: Practical coding for everyone
Here is the link to the explanation - Prefix function - Knuth-Morris-Pratt - Algorithms for Competitive Programming.

if first two characters of a string are same, will the answer be first character always???

Can someone provide c++ code for the logic in the editorial?

Help needed
I have solved this problem using LSP like in KMP matching algorithm but not able to pass all the test case. Below is the link of my code. Please help me.

We can prove that according to the
fact, that our segments built by
moving our pointers cannot ever
intersect. If 2 of them intersecting,
it means that at least one of our
pointers point to some index with
corresponding letter equal to the
string’s first letter. BUT when we
started a substring from the last
appearance of our string’s first
letter, there’s no other appearance in
front of it, so it’s guaranteed that
there will be a mismatch (or it
reached the end of the string).

I didn’t understand this. Can somebody please explain.

I understand the solution now, well also, what is the use of k here? and what is exactly k? isnt ans is k?

This was a good problem for Z algorithm and KMP

I did the same.

Soln with Z-algorithm
https://www.codechef.com/viewsolution/23140613

1 Like

Maybe…

  1. unordered_map
  2. use s.substr instead of pushing every element…

We have exactly same solution XD

I replaced my function of KMP algorithm for pattern searching with yours and it got accepted!

https://www.codechef.com/viewsolution/23141256

Boy I want to get my hands dirty with your resources XD.

XDD how !! I thought we both copied it from gfg…

I have size of LPS=M and you have NN

shouldn’t it be O(|s| * log(|s|)) ?

Yes it should be… Typo… Thanks…

Try to search it on google

O(|S|*log(|S|)) * T must be tight for the time limit.

I think, I had to just break the loop in case of a mismatch, rather just removing those occurances

True @shoom