Given two binary strings s and t. The task is to rearrange the string s in such a way that the occurrence of string t as a sub-string in s is maximum.
Input: s = “101101”, t = “110”
Input: s = “10”, t = “11100”
Input: s = “11000100”, t = “101”
Approach: If we can not make any occurrence of string t in string s then output any permutation of s. Otherwise, start the rearranged string with t. Now we will make the next occurrence of string t in string s as left as possible in order maximize the count of occurrence. To achieve this we will find the largest suffix of string t that matches the prefix of string t
of the same length. It can be found by Prefix Function, Z-Function or Hashes.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Group multiple occurrence of array elements ordered by first occurrence
- Rearrange given binary strings to maximize their Bitwise XOR value
- Rearrange the string to maximize the number of palindromic substrings
- Program to count occurrence of a given character in a string
- Count occurrence of a given character in a string using Stream API in Java
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Find the Nth occurrence of a character in the given String
- Remove the first and last occurrence of a given Character from a String
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Modify given array by incrementing first occurrence of every element by K
- Print the Vowels in the Order of their occurrence in the given Matrix
- Rearrange the given string such that all prime multiple indexes have same character
- Rearrange a string according to the given indices
- Generate two output strings depending upon occurrence of character in input string.
- Generate two output strings depending upon occurrence of character in input string in Python
- Find indices of all occurrence of one string in other
- Minimize the length of string by removing occurrence of only one character
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Print the last occurrence of elements in array in relative order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.