Given a string S of length N, and an array A, consisting of lowercase letters. Also given a positive integer K. the task is to find the minimum number of swaps required (between S and A) to make string S K periodic.
- A string is said to be K periodic, if for each position i in the string S[i] = S[i+K].
- In one move, only one character of S can be swapped with a character of A.
- The characters in A can be used more than once.
Input: S = “nihsiakyt”, K = 3, A = [‘n’, ‘i’, ‘p’, ‘s’, ‘q’]
Considering 0 based positioning for the string S:
Positions 3, 6 should be replaced with ‘n’.
Position 7 should be replaced with ‘i’.
Positions 2, 5, 8 can be replaced with any character from A to make the string K periodic.
Final 3 periodic string: “nisnisnis”
Therefore minimum swaps required = 6
Input: S = “abcdeactr”, K = 4, A = [‘a’, ‘c’, ‘p’]
In total 5 changes are needed to make the string 4 periodic.
- To solve the problem mentioned above we use a 2-dimensional array freq[K] to store frequency of characters at position j % K for all .
- Use a boolean array to mark all characters present in array A.
- For all characters in range 0 to K there will be N / K or (N / K + 1) characters which should be same.
- So for all such characters we check which character has the maximum frequency at position i and is also present in array A.
- Add that to the answer, i.e. .
- We will also add 1 to the answer if because there will be N / K + 1 characters for all such characters i.
Below is the implementation of above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum swaps required to make a binary string alternating
- Minimum number of swaps to make two binary string equal
- Minimum swaps required to convert one binary string to another
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Minimum swaps to make two strings equal by swapping only with third string
- Minimum changes required to make first string substring of second string
- Minimum operations required to make the string satisfy the given condition
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum number of pairs required to make two strings same
- Minimum number of operations required to make two strings equal
- Minimum number of given operations required to make two strings equal
- Minimum number of adjacent swaps to convert a string into its given anagram
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Find the minimum number of preprocess moves required to make two strings equal
- Find the minimum number of operations required to make all array elements equal
- Minimum number of given operations required to convert a string to another string
- Minimum number of subsequences required to convert one string to another
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
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.