5

Compressed String decoding for Kth character

 2 years ago
source link: https://www.geeksforgeeks.org/compressed-string-decoding-for-kth-character/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Compressed String decoding for Kth character

Given a compressed string composed of lowercase characters and numbers, the task is to decode the compressed string and to find out the character located at the Kth position in the uncompressed string, You can transform the string by following the rule that when you encounter a numeric value “n”, repeat the preceding substring starting from index 0 ‘n’ times

Examples:

Input: S = “ab2c3” K = 10
Output: “c”
Explanation:

  • When traversing the string we got the first numeric value 2,
  • here the preceding Substring =” ab “
  • So “ab” is repeated twice, Now the string Will be “ababc3”.
  • The second numeric value we got is 3,
  • Now, our preceding String is “ababc”
  • So it will be repeated 3 times.
  • Now the expanded string will be “ababc” + “ababc” + “ababc” = “ababcababcababc”
  • 10th character is “c”, so the output is “c”.

Input: S = “z3a2s1” K = 12
Output: -1
Explanation: Expanded string will be “zzzazzzas” and have a length of 9, so output -1 (as the Kth index does not exist)

Naive Approach: The basic way to solve the problem is as follows:

The idea is to generate the expanded string and then find the Kth value of that string. To do so we will use a string “decoded” and insert the compressed string when encounter a string and if we encounter a digit then we will multiply the string the digit times and store it in “decoded” string .

Below is the implementation of the above idea:

  • Python3
# Python3 implementation
# of above approach
def getKthCharacter(compressed_str, k):
# To keep track of the length of
# the current expanded string
cur_str_len = 0
# To store the expanded string
expanded_str = ""
for char in compressed_str:
# If the character is a digit
if char.isdigit():
repeat = int(char)
# Repeat the current
# expanded string
expanded_str = expanded_str * repeat
# Update the length
# based on repetition
cur_str_len = cur_str_len * repeat
else:
# If the character is not a digit,
# append it to the expanded string
expanded_str += char
# Increment the length
cur_str_len += 1
# If we have reached the
# desired position
if cur_str_len > k - 1:
# Return the k-th character
# from the expanded string
return expanded_str[k - 1]
# If the position is out of
# bounds, return -1
return -1
# Driver Code
S = "ab2c3"
K = 10
# Call the getKthCharacter function
# and print the result
print(getKthCharacter(S, K))
# This code is contributed by the Author
Output
c

Time Complexity: O(N),
Auxiliary Space: O(N), where N is the length of the expanded string.

Efficient Approach: To solve the problem without expanding the string using Recursion :

  • Initialize a counter to keep track of the current position in the uncompressed string.
  • Iterate through the compressed string’s characters:
    • If a character is a digit, calculate the potential new position in the uncompressed string without expanding the string.
    • If the new position is greater than or equal to K, the Kthcharacter lies within the repeated portion. Recurse on that portion with an adjusted K value.
      • If the character is not a digit, increment the counter.
      • If the counter reaches or exceeds k, return the current character as the k-th character.
  • If the loop completes without finding the k-th character, return -1 to indicate an out-of-bounds position.

Below is the implementation of the above idea:

  • Python3
// C++ code for the above approach:
#include <iostream>
using namespace std;
char getKthCharacterWithoutExpansion(string compressed_str,
int k)
{
// To keep track of the position
// in the uncompressed string
int count = 0;
for (char ch : compressed_str) {
if (isdigit(ch)) {
int repeat = ch - '0';
// Calculate the new position
int new_count = count * repeat;
// If new position exceeds k
if (new_count >= k) {
return getKthCharacterWithoutExpansion(
compressed_str, (k - 1) % count + 1);
}
count = new_count;
}
else {
count += 1;
}
// If we have reached the
// desired position
if (count >= k) {
// Return the current character
return ch;
}
}
// If the position is
// out of bounds
return 0;
}
// Driver code
int main()
{
// Input S and K
string S = "ab2c3";
int K = 15;
// Call the decoding function
// and print the result
char result = getKthCharacterWithoutExpansion(S, K);
(result == 0) ? cout << -1 : cout << result << endl;
return 0;
}
Output
c

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1).

</div


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK