Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Encode Strings in form of “xAyB” where x and y and based on count of digits

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given two numeric string N and M, the task is to encode the given strings in the form “xAyB“, where:

  • x is the count of digits that are same in N and M and are present on same indices
  • y is the count of digits that are same in N and M but are present on different indices

Examples:

Input: N = 123, M = 321
Output: “1A2B”
Explanation: 
Digit 2 satisfies condition for x as count of digits that are same in N and M and are present on same indices
Digits 1 and 3 satisfy the condition for y as count of digits that are same in N and M but are present on different indices

Input: N = 123, M = 111
Output: “0A1B”

 

Approach: The problem can be solved using hashing and two-pointer approach.

  • Convert N and M to string for ease of traversal
  • Now create 2 hash of size 10 to store frequency of digits in N and M respectively
    • Now traverse a loop from 0-9 and:
  • Add min of hashN[i] and hashM[i] to a variable count
  • Now traverse the numbers using two pointers to find the count of digits that are same and occur on same indices in both N and M. Store the count in variable same_dig_cnt
  • Therefore x = same_dig_cnt, y = count.
  • Now return the final string as “xAyB”

Below is the implementation of the above approach:

C++




// C++ implementation of the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to encode strings as "xAyB"
string encodeStrings(int N, int M)
{
 
    // Convert N and M to string
    // for ease of traversal
    string a = to_string(N), b = to_string(M);
 
    // Create 2 hash of size 10
    // to store frequency of digits
    // in N and M respectively
    vector<int> hashN(10, 0);
    for (char c : a)
        hashN++;
    vector<int> hashM(10, 0);
    for (char c : b)
        hashM++;
 
    int count = 0, same_dig_cnt = 0;
 
    // Count of common digits
    // irrespective of their positions
    for (int i = 0; i < 10; i++)
        count += min(hashN[i], hashM[i]);
 
    // Find the count of digits
    // that are same and occur on same indices
    // in both N and M.
    // Store the count in variable same_dig_cnt
    for (int i = 0; i < a.length() && b.length(); i++)
        if (a[i] == b[i])
            same_dig_cnt++;
 
    // Remove the count of digits that are
    // not at same indices in both numbers
    count -= same_dig_cnt;
 
    // Therefore x = same_dig_cnt, y = count.
    // Now return the final string as "xAyB"
    string ans = "" + to_string(same_dig_cnt) + "A"
                 + to_string(count) + "B";
    return ans;
}
 
// Driver code
int main()
{
 
    int N = 1807, M = 7810;
 
    cout << "\"" << encodeStrings(N, M) << "\"";
    return 0;
}


Java




// java implementation of the above approach
class GFG {
 
  // Function to encode Strings as "xAyB"
  static String encodeStrings(int N, int M)
  {
 
    // Convert N and M to String
    // for ease of traversal
    String a = Integer.toString(N), b = Integer.toString(M);
 
    // Create 2 hash of size 10
    // to store frequency of digits
    // in N and M respectively
    int[] hashN = new int[10];
    for (int i = 0; i < 10; i++) {
      hashN[i] = 0;
    }
 
    for(char c : a.toCharArray()) hashN++;
 
    int[] hashM = new int[10];
    for (int i = 0; i < 10; i++) {
      hashM[i] = 0;
    }
 
    for(char c : b.toCharArray()) hashM++;
 
    int count = 0, same_dig_cnt = 0;
 
    // Count of common digits
    // irrespective of their positions
    for (int i = 0; i < 10; i++)
      count += Math.min(hashN[i], hashM[i]);
 
    // Find the count of digits
    // that are same and occur on same indices
    // in both N and M.
    // Store the count in variable same_dig_cnt
    for (int i = 0; i < a.length() && i < b.length(); i++)
      if (a.charAt(i) == b.charAt(i))
        same_dig_cnt++;
 
    // Remove the count of digits that are
    // not at same indices in both numbers
    count -= same_dig_cnt;
 
    // Therefore x = same_dig_cnt, y = count.
    // Now return the final String as "xAyB"
    String ans = "" + Integer.toString(same_dig_cnt) + "A" + Integer.toString(count) + "B";
    return ans;
  }
 
  // Driver code
  public static void main(String args[])
  {
 
    int N = 1807, M = 7810;
 
    System.out.println("\"" + encodeStrings(N, M) + "\"");
  }
}
 
// This code is contributed by Saurabh jaiswal


Python3




# Python code for the above approach
def encodeStrings(N, M):
 
    # Convert N and M to string
    # for ease of traversal
    a = str(N)
    b = str(M)
 
    # Create 2 hash of size 10
    # to store frequency of digits
    # in N and M respectively
    hashN = [0] * 10
    for c in range(len(a)):
        hashN[ord(a) - ord('0')] += 1
 
    hashM = [0] * 10
    for c in range(len(b)):
        hashM[ord(b) - ord('0')] += 1
 
    count = 0
    same_dig_cnt = 0
 
    # Count of common digits
    # irrespective of their positions
    for i in range(10):
        count += min(hashN[i], hashM[i])
 
    # Find the count of digits
    # that are same and occur on same indices
    # in both N and M.
    # Store the count in variable same_dig_cnt
    i = 0
    while(i < len(a) and len(b)):
        if (a[i] == b[i]):
            same_dig_cnt += 1
        i += 1
 
    # Remove the count of digits that are
    # not at same indices in both numbers
    count -= same_dig_cnt
 
    # Therefore x = same_dig_cnt, y = count.
    # Now return the final string as "xAyB"
    ans = str(same_dig_cnt) + "A" + str(count) + "B"
    return ans
 
# Driver code
N = 1807
M = 7810
 
print(f"\"{encodeStrings(N, M)}\"")
 
# This code is contributed by Saurabh jaiswal


C#




// C# implementation of the above approach
using System;
class GFG {
 
  // Function to encode strings as "xAyB"
  static string encodeStrings(int N, int M)
  {
 
    // Convert N and M to string
    // for ease of traversal
    string a = N.ToString(), b = M.ToString();
 
    // Create 2 hash of size 10
    // to store frequency of digits
    // in N and M respectively
    int[] hashN = new int[10];
    for (int i = 0; i < 10; i++) {
      hashN[i] = 0;
    }
 
    foreach(char c in a) hashN++;
 
    int[] hashM = new int[10];
    for (int i = 0; i < 10; i++) {
      hashM[i] = 0;
    }
 
    foreach(char c in b) hashM++;
 
    int count = 0, same_dig_cnt = 0;
 
    // Count of common digits
    // irrespective of their positions
    for (int i = 0; i < 10; i++)
      count += Math.Min(hashN[i], hashM[i]);
 
    // Find the count of digits
    // that are same and occur on same indices
    // in both N and M.
    // Store the count in variable same_dig_cnt
    for (int i = 0; i < a.Length && i < b.Length; i++)
      if (a[i] == b[i])
        same_dig_cnt++;
 
    // Remove the count of digits that are
    // not at same indices in both numbers
    count -= same_dig_cnt;
 
    // Therefore x = same_dig_cnt, y = count.
    // Now return the final string as "xAyB"
    string ans = "" + same_dig_cnt.ToString() + "A"
      + count.ToString() + "B";
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
 
    int N = 1807, M = 7810;
 
    Console.Write("\"" + encodeStrings(N, M) + "\"");
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
        // JavaScript code for the above approach
        function encodeStrings(N, M) {
 
            // Convert N and M to string
            // for ease of traversal
            let a = (N).toString(), b = (M).toString();
 
            // Create 2 hash of size 10
            // to store frequency of digits
            // in N and M respectively
            let hashN = new Array(10).fill(0);
            for (let c = 0; c < a.length; c++)
                hashN[a.charCodeAt(0) - '0'.charCodeAt(0)]++;
            let hashM = new Array(10).fill(0);
            for (c = 0; c < b.length; c++)
                hashM[b.charCodeAt(0) - '0'.charCodeAt(0)]++;
 
            let count = 0, same_dig_cnt = 0;
 
            // Count of common digits
            // irrespective of their positions
            for (let i = 0; i < 10; i++)
                count += Math.min(hashN[i], hashM[i]);
 
            // Find the count of digits
            // that are same and occur on same indices
            // in both N and M.
            // Store the count in variable same_dig_cnt
            for (let i = 0; i < a.length && b.length; i++)
                if (a[i] == b[i])
                    same_dig_cnt++;
 
            // Remove the count of digits that are
            // not at same indices in both numbers
            count -= same_dig_cnt;
             
            // Therefore x = same_dig_cnt, y = count.
            // Now return the final string as "xAyB"
            let ans = (same_dig_cnt).toString() + "A"
                + (count).toString() + "B";
            return ans;
        }
 
        // Driver code
        let N = 1807, M = 7810;
 
        document.write(`"${encodeStrings(N, M)}"`);
         
       // This code is contributed by Potta Lokesh
    </script>


 
 

Output

"1A3B"

 

Time Complexity: O(D), where D is the max count of digits in N or M
Auxiliary Space: O(D), where D is the max count of digits in N or M

 


My Personal Notes arrow_drop_up
Last Updated : 14 Feb, 2022
Like Article
Save Article
Similar Reads
Related Tutorials