Maximum number of consecutive 1's in a string - 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.

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.

Find lexicographically smallest string with given hash value [Competitive Coding]

I encountered the following problem for which I couldn't quite find the appropriate solution.
The problem says for a given string having a specific hash value, find the lowest string (which is not the same as the given one) of the
same length and same hash value (if one exists). E.g. For the
following value mapping of alphabets: {a:0, b:1, c:2,...,z:25}
If the given string is: ady with hash value - 27. The
lexicographically smallest one (from all possible ones excluding the
given one) would be: acz
Solution approach I could think of:
I reduced the problem to Coin-Change problem and resorted to finding all possible combinations for the given sum. Out of all the obtained solutions, I sort them up and find the lowest (or the next smallest if the given string is smallest).
The problem however lies with finding all possible solutions (even in a DP approach) which might be inefficient for larger inputs.
My doubt is:
What solution strategy (possibly even Greedy) could give a better time complexity than above?
I cannot guarantee that this will give you a lower complexity, but a couple of things:1 you don't need to check all the space, just the space of lexicographic value less than or equal to the given string. 2: you can formulate it as an integer programming problem:
Assuming your character space is the letters, and each letter is given its number index[0-25] so a corresponds to 0, b to 1 and so forth. let x_i be the number of letters in your string corresponding to index i. You can formulate your problem as:
min sum_i(wi*xi)
st xi*ai = M
xi>=0,
sum_i(xi)=n
sum_i(wi*xi)<= N
xi integer
Where wi= 26^i, ai is equal to hash(letter(i)), n is the number of letters of the original string, N is the hash value of the original string. This is an integer programming problem so you can try plugging it to a solver. The original problem is very similar to subset sum problem with fixed subset size (where the hash values are the elements you are summing over, and the subset size is the length of the string) so you might also want to take a look at that, although as you will see from the answer it is a complicated problem.

duplicate each space in a given string

I found the following question in many interviews (not my interview).
given a string, you need to replace each space with 2 spaces.
you may assume that your string has enough place for adding the required spaces.
you need to do it in place, memory allocation is not allowed.
I don't understand how to implement this without override letters.
There's not a lot of context in your question. Let's assume it's a programming interview and you are dealing with a low level language like C or assembler. Let's also assume that the string has a count and/or ends in a null, like 'this is a string\0\0\0\0'
I would scan the string from beginning to end and count the spaces, let's call that C. Then I would work backward through the string on character at a time moving each character forward by C positions. Each time a space is encountered, copy the space forward by C positions, subtract one from C, and then move the space by C positions. Stop when C is 0.
Here, nulls/unused are represented by a period.
this is a string.... C=3
this is a string..g.
this is a string.ng.
this is a stringing.
this is a strinring.
this is a stritring.
this is a strstring.
this is a st string.
this is a s string. C=2
this is a a string.
this is a string.
this iss a string. C=1
this iis a string.
this is a string. C=0
Shifting the string following a found space is required by one letter. To reduce time needed to reduce the shifting part, I would use this approach:
Count the number of space. I will call this count c.
Shift the string to the number of spaces to the right (I'm assuming here a left to right reading direction.)
Start a loop starting at offset c until the end of the string:
Initialize a counter for the already duplicated space, called s, with 0
Copy the letter at current position to current position - c + s
If the letter was a space, increment s and add a space to position - c + 1
Not sure if all the offset are correctly calculated in my mind, correct it if needed. But because this is just an interview question to idea is just to sketch a correct algorithm.

How to make string a palindrome with minimum number of shifts?

I am given a string for example aaabb and i have to make it a palindrome with minimum number of shifts,what algorithm should i use to achieve that?
https://www.hackerrank.com/contests/sample-1-1/challenges/cyclic-palindrome
Please no code only algorithm
Idea 1:
You can use polynomial hashing to compute the hash of the shifted string in O(1) using the prefix sums of hashes for the initial string and for its reversed version. After that, you need to check that these two hashes are equal. You can just test all possible shifts and choose the smallest one that fits (the total time complexity is O(N)).
Idea 2:
Let's write the string twice. We need to find a palindrome of at least N characters. You can use Manacher's algorithm to find all such palindromes in linear time and choose the one that corresponds to the smallest shift.
Both solution have a linear time complexity, but the first one is slightly easier to code, while the second one is guaranteed to work for all possible inputs as it doesn't rely on lack of hash collisions.
Interesting thing to notice in palindrome string is there can be at most 1 type of character with odd frequency. i.e., abcccba a(2) b(2) c(3).
So while scanning string, maintain count of characters. If odd_count > 1, print -1
If odd_count = 1, you need to keep this type of character in middle, so shift string minimum times (either left or right shift) and check if it forms palindrome.
If odd_count = 0, you need to shift and check.

Delete all ocurrences of substring in minimal steps

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

Resources