Delete all ocurrences of substring in minimal steps - string

I want to find the minimum number of deletions I need to make in order for a substring to no longer appear in a given string. Both the string and substring are composed of only lower case letters.
For example, for string "recorerecore" and substring "recore" I would need 2 deletions.
For string "recorecore" and substring "recore" I would need only 1.
For string "recorecorecorecore" and substring "recore" I would need 2, either the first and third or the second and fourth.
For string "rerecorecore" I would need to take out 1, the second occurrence, as taking the first out would lead to having recore again.
I only can think of the brute force solution which involves actually deleting in every combination possible and finding the minimum, but this takes too long.
Does anyone know a way to do this faster?

recursively Boyer–Moore the string with the substring and delete as you find them

Related

Check if string contains consecutive repeated substring

I got an interview problem which asks to determine whether or not a given string contains substring repeated right after it. For example:
ATAYTAYUV contains TAY after TAY
AABCD contains A after A
ABCAB contains two AB, but they are not consecutive, so the answer is negative
My idea was to look at the first letter, find its second occurrence then check letter by letter if the letters after the first occurrence match the letters after the second occurrence. If they all do, the answer is positive. If not, once I get a mismatch, I can repeat the process but starting with the last letter I checked, since I would not be able to get a repeated sequence up to that point.
I am not sure if the approach is correct or if it is the mos efficient.
Assume that you are looking for a repeating pattern of length 3. If you write the string shifted right by three positions in front of itself (and trimmed), you can detect runs of 3 identical characters.
ATAYTAYUV
ATAYTA
Repeat this for all lengths up to N/2.

Maximum number of consecutive 1's in a string

A string of length N (can be upto 10^5) is given which consists of only 0 and 1. We have to remove two substrings of length exactly K from the original string to maximize the number of consecutive 1's.
For example suppose the string is 1100110001and K=1.
So we can remove two substrings of length 1. The best possible option here is to remove the 0's at 3rd place and 4th place and get the output as 4 (as the new string will be 11110001)
If I try brute force it'll timeout for sure. I don't know if sliding window will work or not. Can anyone give me any hint on how to proceed? I am not demanding the full answer obviously, just some hints will work for me. Thanks in advance :)
This has a pretty straightforward dynamic programming solution.
For each index i, calculate:
The length of the sequence of 1s that immediately precedes it, if nothing has been removed;
The longest sequence of 1s that could immediately precede it, if exactly one substring is removed before it; and
The longest sequence of 1s that could immediately precede it, if exactly two substrings are removed before it.
For each index, these three values are easily calculated in constant time from the values for earlier indexes, so you can do this in a single pass in O(N) time.
For example, let BEST(i,r) be the best length immediately preceding position i after removing r substrings. If i >= K, then you can remove a substring ending at i and have BEST(i,r) = BEST(i-K,r-1) for r > 0. If string[i-1] = '1' then you could extend the sequence from the previous position and have BEST(i,r) = BEST(i-1,r)+1. Choose the best possibility for each i,r.
The largest value you find in step (3) is the answer.

Find Sub-string that create whole string by repetation

I have string "hrhrhrhrhr".
I want to find smallest sub-string of t such that we can make whole string by appending that sub-string in itself several time.
in this example i can make string "hrhrhrhrhr" by four time appending of "hr" with itself.
how to find this kind of substring?
fox example,
"abcabcabc" then "abc" is answer.
"ttttttt" -> "t" is answer.
"abcd" -> "abcd" is answer.
which algorithm or specific method i should use?
I would suggest you to take a look at string matching/search algorithms. Particularly, if you use KMP (Knuth-Morris Pratt) algorithm to search the string in itself, the lookup table would yield the pattern. In addition, the highest number in the table would give you the end character of the substring you are searching for (if the string is indeed composed of the repetition of one substring).

How should I remove all the repeated words and letters of a String?

I am trying to remove every character repeated over 2 times from an extremely long string. So, for example, the word Terrrrrrific becomes Terrific.
Now my question is, how do I filter out repeats that include more than a single character the same way, i.e. if I have Words words words words words I want to filter it down to words words, however, it might be something less sensible, such as abcdabcdabcdabcdabcd which should become abcdabcd.
I do suspect that I should use a suffix tree, but I'm not sure how to go at the algorithm exactly.
I don't know, Is this efficient algorithm for you but you can do this:
Choose length for finding repeats
Then for every start point from 0 to length-1 go through string
Maintain stack (you use disjoint substrings and push on stack if top two from stack is different from them)

String manipulation: randomly swapping out the first letter with one of the other letters of the word

I need to randomly swap out the first letter of a word with one of the other letters. What i am having trouble is with specifying that i only need to randomly generate ONE character. I cant use any conditionals, so can anyone please recommend a method to use?

Resources