Encode Strings in form of “xAyB” where x and y and based on count of digits
Last Updated :
13 Jul, 2023
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: “1A0B”
Approach:
- Convert the given numeric strings N and M to integers n and m.
- Define a function
encode
that 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
sameDigits
and sameIndex
to 0.
- Create a new Set called
set
and add each character in s1 to it.
- For each character c in s2, if c is in set, increment
sameDigits
and 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
sameIndex
and sameDigits
– sameIndex
in the form “xAyB”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string encode( int n, int m)
{
string s1 = to_string(n), s2 = to_string(m);
int sameDigits = 0, sameIndex = 0;
unordered_set< char > set;
for ( char c : s1)
set.insert(c);
for ( char c : s2)
if (set.erase(c))
sameDigits++;
for ( int i = 0; i < s1.length(); i++) {
if (s1[i] == s2[i])
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;
}
|
Java
import java.util.HashSet;
import java.util.Set;
class GFG {
public static String encode( int n, int m)
{
String s1 = String.valueOf(n), s2
= String.valueOf(m);
int sameDigits = 0 , sameIndex = 0 ;
Set<Character> set = new HashSet<>();
for ( char c : s1.toCharArray())
set.add(c);
for ( char c : s2.toCharArray())
if (set.remove(c))
sameDigits++;
for ( int i = 0 ; i < s1.length(); i++) {
if (s1.charAt(i) == s2.charAt(i))
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 ));
}
}
|
Python3
def encode(n: int , m: int ) - > str :
s1, s2 = str (n), str (m)
sameDigits = sameIndex = 0
set_ = set (s1)
for c in s2:
if c in set_:
sameDigits + = 1
set_.remove(c)
for i in range ( len (s1)):
if s1[i] = = s2[i]:
sameIndex + = 1
return f "{sameIndex}A{sameDigits - sameIndex}B"
print (encode( 123 , 321 ))
print (encode( 1807 , 7810 ))
print (encode( 123 , 111 ))
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static string encode( int n, int m)
{
string s1 = n.ToString(), s2 = m.ToString();
int sameDigits = 0, sameIndex = 0;
HashSet< char > set = new HashSet< char >();
foreach ( char c in s1) set .Add(c);
foreach ( char c in s2) if ( set .Remove(c))
sameDigits++;
for ( int i = 0; i < s1.Length; i++) {
if (s1[i] == s2[i])
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));
}
}
|
Javascript
<script>
function encode(n, m) {
let [s1,s2] = [n.toString(),m.toString()];
let sameDigits = 0, sameIndex = 0;
let set = new Set(s1);
for (let c of s2) {
if (set.has(c)) {
sameDigits++;
set. delete (c);
}
}
for (let i = 0; i < s1.length; i++) {
if (s1[i] === s2[i]) 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
1A0B
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...