
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find k-th character of decrypted string - Set – 2 in C++
Concept
With respect of a given encoded string where repetitions of substrings are indicated as substring followed by count of substrings. So, for example, if encrypted string is “pq2rs2” and k=5, so output will be ‘r’ because decrypted string is “pqpqrsrs” and 5th character is ‘r’.
It should be noted that frequency of encrypted substring can be of more than one digit. So, for example, in “pq12r3”, pq is repeated 12 times. Here, no leading 0 is present in frequency of substring.
Input
"p2q2r3", k = 6
Output
r
Decrypted string is "ppqqrrr"
Input
"pq4r2ts3", k = 11
Output
t
Decrypted string is "pqpqpqpqrrtststs"
Method
Here, the stepwise algorithm is −
- Determine length of current substring. Implement two pointers. We have to fix one pointer at starting of substring and proceed another pointer until a digit is not found.
- Determine frequency of repetition by moving the second pointer further until an alphabet is not found.
- Determine length of substring if it is repeated by multiplying frequency and its original length.
It has been observed that if this length is smaller than k, then required character lies in substring that follows. We have to subtract this length from k to maintain count of number of characters that are required to be covered.
It has been seen that if length is smaller than or equal to k, then required character lies in current substring. Because k is 1-indexed, decrease it by 1 and then take its mod with original substring length. Here, required character is kth character from beginning of substring which is pointed by first pointer.
Example
// C++ program to find K'th character in // decrypted string #include <bits/stdc++.h> using namespace std; // Shows function to find K'th character in // Encoded String char encodedChar(string str, int k){ int a, b; int m = str.length(); // Used to store length of substring int len1; // Used to store length of substring when // it is repeated int num1; // Used to store frequency of substring int freq1; a = 0; while (a < m) { b = a; len1 = 0; freq1 = 0; // Determine length of substring by // traversing the string until // no digit is found. while (b < m && isalpha(str[b])) { b++; len1++; } // Determine frequency of preceding substring. while (b < m && isdigit(str[b])) { freq1 = freq1 * 10 + (str[b] - '0'); b++; } // Determine length of substring when // it is repeated. num1 = freq1 * len1; // It has been seen that if length of repeated substring is less than // k then required character is present in next // substring. Subtract length of repeated // substring from k to keep account of number of // characters required to be visited. if (k > num1) { k -= num1; a = b; } // It has been seen that if length of repeated substring is // more or equal to k then required // character lies in current substring. else { k--; k %= len1; return str[a + k]; } } // This is for the case when there // are no repetition in string. // e.g. str="abced". return str[k - 1]; } // Driver Code int main(){ string str1 = "pqpqpqpqrrtststs"; int k1 = 11; cout << encodedChar(str1, k1) << endl; string str2 = "p2q2r3"; int k2 = 6; cout << encodedChar(str2, k2) << endl; return 0; }
Output
t r
- Related Articles
- Find k-th character of decrypted string - Set – 2 in Python
- Find k’th character of decrypted string in C++
- Position of the K-th set bit in a number in C++
- Find K-th Smallest Pair Distance in C++
- Find value of k-th bit in binary representation in C++
- C# program to remove n-th character from a string
- The k-th Lexicographical String of All Happy Strings of Length n in C++
- Find k-th bit in a binary string created by repeated invert and append operations in C++
- Product of nodes at k-th level in a tree represented as string in C++
- Find k-th smallest element in given n ranges in C++
- Find m-th smallest value in k sorted arrays in C++
- K-th Symbol in Grammar in C++
- Find the k-th smallest divisor of a natural number N in C++
- C# program to replace n-th character from a given index in a string
- K-th Element of Two Sorted Arrays in C++
