Question: Strings with long blocks of repeating characters take much less space if kept in a compressed representation. To obtain the compressed representation, we replace each
Strings with long blocks of repeating characters take much less space if kept in a compressed representation. To obtain the compressed representation, we replace each segment of equal characters in the string with the number of characters in the segment followed by the character for example, we replace segment CCCC with C To avoid increasing the size, we leave the oneletter segments unchanged the compressed representation of BC is the same string BC For example, the compressed representation of the string "ABBBCCDDCCC" is ABCDC and the compressed representation of the string "AAAAAAAAAAABXXAAAAAAAAAA" is ABXA ABBBCCDDCCC ABCDC ABXA Observe that, in the second example, if we removed the BXX segment from the middle of the word, we would obtain a much shorter compressed representation A In order to take advantage of this observation, we decided to modify our compression algorithm. Now, before compression, we remove exactly K consecutive letters from the input string. We would like to know the shortest compressed form that we can generate this way. Write a function: class Solution public int solutionString int k; that, given a string S of length N and an integer K returns the shortest possible length of the compressed representation of S after removing exactly K consecutive characters from S Examples: Given S"ABBBCCDDCCC and K the function should return because after removing DDC from S we are left with "ABBBCCCC", which compresses to a representation of length ABC ABBBCCDDCCC ABC Given S "AAAAAAAAAABXXAAAAAAAAAA" and K the function should return because after removing BXX from S we are left with "AAAAAAAAAAAAAAAAAAAAA", which compresses to a representation of length Given S "ABCDDDEFG" and K the function should return because after removing EF from S we are left with "ABCDDDG", which compresses to a representation of length "ABCDG ABCDDDEFG ABCDG Write an efficient algorithm for the following assumptions: N is an integer within the range ; K is an integer within the range ; KSN; string S consists only of uppercase letters AZ
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
