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:
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.
- Minimum swaps required to make a binary string alternating
- Minimum swaps required to make a binary string divisible by 2^k
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- 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 number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum swaps to make two strings equal by swapping only with third string
- Check if the given string is K-periodic
- Minimum changes required to make first string substring of second string
- Smallest number greater than X which is K-periodic
- Minimum number of adjacent swaps to convert a string into its given anagram
- Count distinct regular bracket sequences which are not N periodic
- Minimum operations required to make the string satisfy the given condition
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Minimum number of given operations required to make two strings equal
- 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
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.