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 indicesInput: N = 123, M = 111
Output: "1A0B"
Approach:
- Convert the given numeric strings N and M to integers n and m.
- Define a function
encodethat takes two integers n and m as input and returns a string in the form “xAyB”. - Inside the function, convert n and m to strings s1 and s2.
- Initialize two variables
sameDigitsandsameIndexto 0. - Create a new Set called
setand add each character in s1 to it. - For each character c in s2, if c is in set, increment
sameDigitsand remove c from set. - For each index i in s1 and s2, if the characters at index i are equal, increment
sameIndex. - Return a formatted string containing
sameIndexandsameDigits-sameIndexin the form “xAyB”.
Below is the implementation of the above approach:
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
// This function takes two integers n and m as input and
// returns a string.
string encode(int n, int m)
{
// Convert n and m to strings.
string s1 = to_string(n), s2 = to_string(m);
// Initialize sameDigits and sameIndex to 0.
int sameDigits = 0, sameIndex = 0;
// Create a new unordered_set called set.
unordered_set<char> set;
// Add each character in s1 to set.
for (char c : s1)
set.insert(c);
// For each character c in s2, if c is in set, increment
// sameDigits.
for (char c : s2)
if (set.erase(c))
sameDigits++;
// For each index i in s1 and s2, if the characters at
// index i are equal, increment sameIndex.
for (int i = 0; i < s1.length(); i++) {
if (s1[i] == s2[i])
sameIndex++;
}
// Return a formatted string containing sameIndex and
// sameDigits - sameIndex.
return to_string(sameIndex) + "A"
+ to_string(sameDigits - sameIndex) + "B";
}
int main()
{
cout << encode(123, 321) << endl;
cout << encode(1807, 7810) << endl;
cout << encode(123, 111) << endl;
}
import java.util.HashSet;
import java.util.Set;
class GFG {
// This function takes two integers n and m as input and
// returns a string.
public static String encode(int n, int m)
{
// Convert n and m to strings.
String s1 = String.valueOf(n), s2
= String.valueOf(m);
// Initialize sameDigits and sameIndex to 0.
int sameDigits = 0, sameIndex = 0;
// Create a new HashSet called set.
Set<Character> set = new HashSet<>();
// Add each character in s1 to set.
for (char c : s1.toCharArray())
set.add(c);
// For each character c in s2, if c is in set,
// increment sameDigits.
for (char c : s2.toCharArray())
if (set.remove(c))
sameDigits++;
// For each index i in s1 and s2, if the characters
// at index i are equal, increment sameIndex.
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == s2.charAt(i))
sameIndex++;
}
// Return a formatted string containing sameIndex
// and sameDigits - sameIndex.
return String.format("%dA%dB", sameIndex,
sameDigits - sameIndex);
}
public static void main(String[] args)
{
System.out.println(encode(123, 321));
System.out.println(encode(1807, 7810));
System.out.println(encode(123, 111));
}
}
# This function takes two integers n and m as input and returns a string.
def encode(n: int, m: int) -> str:
# Convert n and m to strings.
s1, s2 = str(n), str(m)
# Initialize sameDigits and sameIndex to 0.
sameDigits = sameIndex = 0
# Create a new set called set_.
set_ = set(s1)
# For each character c in s2, if c is in set_, increment sameDigits.
for c in s2:
if c in set_:
sameDigits += 1
set_.remove(c)
# For each index i in s1 and s2, if the characters at index i are equal, increment sameIndex.
for i in range(len(s1)):
if s1[i] == s2[i]:
sameIndex += 1
# Return a formatted string containing sameIndex and sameDigits - sameIndex.
return f"{sameIndex}A{sameDigits - sameIndex}B"
print(encode(123, 321))
print(encode(1807, 7810))
print(encode(123, 111))
using System;
using System.Collections.Generic;
class GFG {
// This function takes two integers n and m as input and
// returns a string.
public static string encode(int n, int m)
{
// Convert n and m to strings.
string s1 = n.ToString(), s2 = m.ToString();
// Initialize sameDigits and sameIndex to 0.
int sameDigits = 0, sameIndex = 0;
// Create a new HashSet called set.
HashSet<char> set = new HashSet<char>();
// Add each character in s1 to set.
foreach(char c in s1) set.Add(c);
// For each character c in s2, if c is in set,
// increment sameDigits.
foreach(char c in s2) if (set.Remove(c))
sameDigits++;
// For each index i in s1 and s2, if the characters
// at index i are equal, increment sameIndex.
for (int i = 0; i < s1.Length; i++) {
if (s1[i] == s2[i])
sameIndex++;
}
// Return a formatted string containing sameIndex
// and sameDigits - sameIndex.
return $ "{sameIndex}A{sameDigits - sameIndex}B";
}
public static void Main(string[] args)
{
Console.WriteLine(encode(123, 321));
Console.WriteLine(encode(1807, 7810));
Console.WriteLine(encode(123, 111));
}
}
<script>
// This function takes two integers n and m as input and returns a string.
function encode(n, m) {
// Convert n and m to strings.
let [s1,s2] = [n.toString(),m.toString()];
// Initialize sameDigits and sameIndex to 0.
let sameDigits = 0, sameIndex = 0;
// Create a new Set called set.
let set = new Set(s1);
// For each character c in s2, if c is in set, increment sameDigits.
for (let c of s2) {
if (set.has(c)) {
sameDigits++;
set.delete(c);
}
}
// For each index i in s1 and s2, if the characters at index i are equal, increment sameIndex.
for (let i = 0; i < s1.length; i++) {
if (s1[i] === s2[i]) sameIndex++;
}
// Return a formatted string containing sameIndex and sameDigits - sameIndex.
return `${sameIndex}A${sameDigits - sameIndex}B`;
}
document.write(encode(123, 321) + "<br>");
document.write(encode(1807, 7810) + "<br>");
document.write(encode(123, 111) + "<br>");
</script>
Output:
1A2B
1A3B
1A0BTime 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