![]() |
VOOZH | about |
Given a string 'S' of length N which consists of both Uppercase and Lowercase English alphabets and a positive integer K (1 <= K <= N), the task is to return the length of the resultant String after deleting all the Invalid substrings which are of length K from the given String. A String Is known as an Invalid String if every letter in String is present in both uppercase and lowercase with equal frequency with the length K then String is known as Invalid String.
Note: After Deleting all the Invalid substrings in the given string if the resultant string still contains invalid substrings don't do any modifications just return the length of the resultant string.
Examples:
Input: S = "abcdaADCf", K = 2
Output: 7
Explaination:
- In the above example, there is only one invalid string of length 2 it is "aA".
- Because among every substring of length K in the given original string "aA" is the only invalid substring we need to delete it.
- After removing it the resultant string is "abcdDCf".
- Here in the above resultant string, there is an invalid string of length 2 it is "dD", but no modification should be done to the resultant string.
- Hence "dD" should not be deleted and the length of the resultant String is 7.
Input: S = "xyzAdaD", K = 4
Output: 3
Explanation: The Substring "AdaD" is the only Invalid Substring and after deleting the Invalid Substring the String becomes "xyz". Hence The Length is 3.
Approach: To Solve The Problem we can use the concept of sliding window algorithm as follows:
By using Sliding Window we can calculate the total length of all invalid substrings in given string, by removing these length from original string length we can get our required answer.
Follow the steps to solve the problem:
Below is the code to implement the above approach:
3
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N), where N is the length of the string.