Wednesday, October 9, 2024
Google search engine
HomeData Modelling & AICompressed String decoding for Kth character

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:

C++




#include <iostream>
#include <string>
Ā 
// Function to find the k-th character in a compressed string
char getKthCharacter(std::string compressedStr, int k) {
Ā Ā Ā Ā std::string expandedStr;
Ā Ā Ā Ā int curStrLen = 0;
Ā 
Ā Ā Ā Ā for (char c : compressedStr) {
Ā Ā Ā Ā Ā Ā Ā Ā if (std::isdigit(c)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int repeat = c - '0'; // Extract the digit (repetition count)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Repeat the current expanded string based on the repetition count
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā std::string repeated(expandedStr);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i < repeat; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā expandedStr += repeated;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the length based on repetition
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curStrLen *= repeat;
Ā Ā Ā Ā Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the character is not a digit, simply append it to the expanded string
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā expandedStr += c;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Increment the length
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curStrLen++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (curStrLen >= k) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return expandedStr[k - 1]; // If we have reached the desired position,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // return the k-th character
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return '\0'; // If the position is out of bounds, return a default value
}
Ā 
int main() {
Ā Ā Ā Ā std::string S = "ab2c3";
Ā Ā Ā Ā int K = 10;
Ā 
Ā Ā Ā Ā char result = getKthCharacter(S, K);
Ā 
Ā Ā Ā Ā if (result == '\0') {
Ā Ā Ā Ā Ā Ā Ā Ā std::cout << "Position is out of bounds." << std::endl;
Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā Ā Ā std::cout << "The K-th character is: " << result << std::endl;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return 0;
}


Java




public class KthCharacterInCompressedString {
Ā Ā Ā Ā // Function to find the k-th character in a compressed string
Ā Ā Ā Ā public static char getKthCharacter(String compressedStr, int k) {
Ā Ā Ā Ā Ā Ā Ā Ā int curStrLen = 0; // To keep track of the length of the current expanded string
Ā Ā Ā Ā Ā Ā Ā Ā StringBuilder expandedStr = new StringBuilder(); // To store the expanded string
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā for (char c : compressedStr.toCharArray()) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (Character.isDigit(c)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int repeat = Character.getNumericValue(c); // Extract the digit (repetition count)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Repeat the current expanded string based on the repetition count
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā expandedStr = new StringBuilder(expandedStr.toString().repeat(repeat));
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the length based on repetition
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curStrLen *= repeat;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the character is not a digit, simply append it to the expanded string
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā expandedStr.append(c);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Increment the length
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curStrLen++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (curStrLen > k - 1) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return expandedStr.charAt(k - 1); // If we have reached the desired
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // position, return the k-th character
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return '\0'; // If the position is out of bounds, return a
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // default value (you can change this to -1 if needed).
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public static void main(String[] args) {
Ā Ā Ā Ā Ā Ā Ā Ā String S = "ab2c3";
Ā Ā Ā Ā Ā Ā Ā Ā int K = 10;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā char result = getKthCharacter(S, K);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (result == '\0') {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Position is out of bounds.");
Ā Ā Ā Ā Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("The K-th character is: " + result);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}


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


C#




using System;
Ā 
class GFG
{
Ā Ā Ā Ā // Function to find the k-th character in a compressed string
Ā Ā Ā Ā static char GetKthCharacter(string compressedStr, int k)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā string expandedStr = "";
Ā Ā Ā Ā Ā Ā Ā Ā int curStrLen = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā foreach (char c in compressedStr)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (char.IsDigit(c))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int repeat = c - '0'; // Extract the digit (repetition count)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Repeat the current expanded string based on the repetition count
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā string repeated = expandedStr;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i < repeat; i++)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā expandedStr += repeated;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the length based on repetition
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curStrLen *= repeat;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the character is not a digit, simply append it to the expanded string
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā expandedStr += c;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Increment the length
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curStrLen++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (curStrLen >= k)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return expandedStr[k - 1]; // If we have reached the desired position,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // return the k-th character
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return '\0'; // If the position is out of bounds, return a default value
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā static void Main()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā string S = "ab2c3";
Ā Ā Ā Ā Ā Ā Ā Ā int K = 10;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā char result = GetKthCharacter(S, K);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (result == '\0')
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("Position is out of bounds.");
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine($"The K-th character is: {result}");
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}


Javascript




// JavaScript implementation
// of above approach
Ā 
function getKthCharacter(compressed_str, k) {
Ā 
Ā Ā // To keep track of the length of
Ā Ā // the current expanded string
Ā Ā let cur_str_len = 0;
Ā 
Ā Ā // To store the expanded string
Ā Ā let expanded_str = "";
Ā 
Ā Ā for (let char of compressed_str) {
Ā 
Ā Ā Ā Ā // If the character is a digit
Ā Ā Ā Ā if (!isNaN(char)) {
Ā Ā Ā Ā Ā Ā let repeat = parseInt(char);
Ā 
Ā Ā Ā Ā Ā Ā // Repeat the current
Ā Ā Ā Ā Ā Ā // expanded string
Ā Ā Ā Ā Ā Ā expanded_str = expanded_str.repeat(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
let S = "ab2c3";
let K = 10;
Ā 
// Call the getKthCharacter function
// and print the result
console.log(getKthCharacter(S, K));
Ā 
// This code is contributed by Tapesh(tapeshdua420)


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 Kth character 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:

C++




// 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;
}


Java




//Java code for the above approach
public class GFG {
Ā Ā Ā Ā public static 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.toCharArray()) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (Character.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';
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public static void main(String[] args) {
Ā Ā Ā Ā Ā Ā Ā Ā // Input S and K
Ā Ā Ā Ā Ā Ā Ā Ā String S = "ab2c3";
Ā Ā Ā Ā Ā Ā Ā Ā int K = 15;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Call the decoding function and print the result
Ā Ā Ā Ā Ā Ā Ā Ā char result = getKthCharacterWithoutExpansion(S, K);
Ā Ā Ā Ā Ā Ā Ā Ā if (result == '\0') {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(-1);
Ā Ā Ā Ā Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(result);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}


Python3




# Python3 implementation
# of above approach
Ā 
Ā 
def getKthCharacterWithoutExpansion(compressed_str, k):
Ā Ā Ā # To keep track of the position
Ā Ā Ā Ā # in the uncompressed string
Ā Ā Ā Ā count = 0
Ā 
Ā Ā Ā Ā for char in compressed_str:
Ā Ā Ā Ā Ā Ā Ā Ā if char.isdigit():
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā repeat = int(char)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Calculate the new position
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 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 char
Ā Ā Ā Ā return -1
Ā 
Ā 
# Driver Code
S = "ab2c3"
K = 15
Ā 
# Call the getKthCharacter
# WithoutExpansion function
# And print the result
print(getKthCharacterWithoutExpansion(S, K))
Ā 
# This code is contributed by the Author


C#




using System;
Ā 
class Program
{
Ā Ā Ā Ā static void Main()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā string S = "ab2c3";
Ā Ā Ā Ā Ā Ā Ā Ā int K = 15;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Call the GetKthCharacterWithoutExpansion function
Ā Ā Ā Ā Ā Ā Ā Ā // And print the result
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(GetKthCharacterWithoutExpansion(S, K));
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā static char GetKthCharacterWithoutExpansion(string compressedStr, int k)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // To keep track of the position
Ā Ā Ā Ā Ā Ā Ā Ā // in the uncompressed string
Ā Ā Ā Ā Ā Ā Ā Ā int count = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā foreach (char c in compressedStr)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (char.IsDigit(c))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int repeat = int.Parse(c.ToString());
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Calculate the new position
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int newCount = count * repeat;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If new position exceeds k
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (newCount >= k)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return GetKthCharacterWithoutExpansion(compressedStr, (k - 1) % count + 1);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count = newCount;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If we have reached the desired position
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (count >= k)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Return the current character
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return c;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return '\0';
Ā Ā Ā Ā }
}


Javascript




function main() {
Ā Ā Ā Ā const S = "ab2c3";
Ā Ā Ā Ā const K = 15;
Ā 
Ā Ā Ā Ā // Call the GetKthCharacterWithoutExpansion function
Ā Ā Ā Ā // And print the result
Ā Ā Ā Ā console.log(GetKthCharacterWithoutExpansion(S, K));
}
Ā 
function GetKthCharacterWithoutExpansion(compressedStr, k) {
Ā Ā Ā Ā // To keep track of the position in the uncompressed string
Ā Ā Ā Ā let count = 0;
Ā 
Ā Ā Ā Ā for (let i = 0; i < compressedStr.length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā const c = compressedStr.charAt(i);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (!isNaN(parseInt(c))) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā const repeat = parseInt(c);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Calculate the new position
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā const newCount = count * repeat;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If new position exceeds k
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (newCount >= k) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return GetKthCharacterWithoutExpansion(compressedStr, (k - 1) % count + 1);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count = newCount;
Ā Ā Ā Ā Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If we have reached the desired position
Ā Ā Ā Ā Ā Ā Ā Ā if (count >= k) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Return the current character
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return c;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return '\0';
}
Ā 
main(); // Call the main function


Output

c













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

Feeling lost in the world of random DSA topics, wasting time without progress? Itā€™s time for a change! Join our DSA course, where weā€™ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfGā€™s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
13 Dec, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?