- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Encode Strings in form of “xAyB” where x and y and based on count of digits
In this problem, we need to encode the string in xAyB format, where x is the count of digits present in both strings at the same index, and y is the count of digits in both strings at different indices.
We can solve the problem by counting the same digits in both strings. Also, we can count the total number of the same digits present at same index in both strings to encode the string.
Problem statement - We have given str1 and str2 strings of the same length containing only digits. We need to encode the given string in the xAyB format. Here, A and B is contstant.
X is the total number of digits that are the same and exists at the same index in str1 and str2.
Y is the total number of digits that are the same and exists at different indices in str1 and str2.
Sample examples
Input
str1 = 4231, str2 = 4623
Output
1A2B
Explanation - In the input strings, '2' is a common digit and exists at the same index in both strings.
In both strings, '4' and '3' are also common which are exist at different index.
Input
str1 = 1234, str2 = 1234
Output
4A0B
Explanation - In the given string, all characters are the same and exist at the same index. So, x is 4 and y is 0.
Input
str1 = 6543, str2 = 3456;
Output
0A4B
Explanation - The given string contains all the same characters but present at different indices. So, x is 0 and y is 4.
Approach 1
In this approach, we will find the counts of the same digits in both strings. After that, we will find counts of the same digits present at the same index, and using that, we can count the total count of the same digits present at different indices.
Algorithm
Step 1 - Convert integers to strings using the toString() method.
Step 2 - Initialize the freq1 and freq2 list of size 10 with 0 to store the frequency of digits in str1 and str2.
Step 3 - Traverse the str1 and str2 one after another to count the frequency of digits.
Step 4 - Initialize the 'sameDigits' variable with zero to store the count of the same digits in both strings and 'sameIndex' with zero to store the count of the same digits present at the same index.
Step 5 - Make 0 to 9 iterations, and add a minimum of freq1[p] and freq2[p] to the 'sameDigits' variable's value.
Step 6 - Now, traverse both strings and if any character is the same at the pth index in both strings, increase the value of 'sameIndex' by 1.
Step 7 - Subtract the sameIndex from sameDigits.
Step 8 - Encode the string. Here, sameIndex is the value of x, and sameDigits is the value of y.
Step 9 - Return the encoded string.
Example
#include <bits/stdc++.h> using namespace std; string getEncodedStr(int str1, int str2) { // Conver numeric strings to strings string temp1 = to_string(str1), temp2 = to_string(str2); // Calculate sameDigits of digits in str1 vector<int> freq1(10, 0); for (char ch : temp1) freq1[ch - '0']++; // Calculate sameDigits of digits in str2 vector<int> freq2(10, 0); for (char ch : temp2) freq2[ch - '0']++; int sameDigits = 0, sameIndex = 0; // Find the total number of counts of the same digits for (int p = 0; p < 10; p++) sameDigits += min(freq1[p], freq2[p]); // Find the total number of counts of the same digits on the same indices for (int p = 0; p < temp1.length() && temp2.length(); p++) { if (temp1[p] == temp2[p]) { sameIndex++; } } // Get the total number of the same digits on different indices sameDigits -= sameIndex; // Encode the string string ans = "" + to_string(sameIndex) + "A" + to_string(sameDigits) + "B"; return ans; } int main() { int str1 = 4231, str2 = 4623; cout << "The encoded string is - " << getEncodedStr(str1, str2) << endl; return 0; }
Output
The encoded string is - 1A2B
Time complexity - O(N) to traverse the string and count digits frequency.
Space complexity - O(1), as we use constant space to count the frequency of 10 digits.
We learned to encode the string by counting the same digits present at the same and different indexes. Programmers may try to encode the string by using the sum of digits present at the same or different indices.