# Count of digits in N and M that are same and are present on the same indices

• Last Updated : 09 Feb, 2022

Given two number N and M, the task is to find the count of digits in N and M that are the same and are present on the same indices.

Examples:

Input: N = 123, M = 321
Output: 1
Explanation: Digit 2 satisfies the condition

Input: N = 123, M = 111
Output: 1

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

• Convert N and M for ease of traversal
• Create two pointers where one pointer points to the first digit of N and the other to the first digit of M respectively initially.
• Now traverse both the numbers left to right and check if the digits on both pointers are same or not.
• If yes, increase the count.
• Increase the pointer by 1 on each iteration.
• Return the final count at the end.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach` `#include ``using` `namespace` `std;``int` `countDigits(``int` `N, ``int` `M)``{` `    ``// Convert N and M to string``    ``// for ease of traversal``    ``string a = to_string(N), b = to_string(M);` `    ``int` `same_dig_cnt = 0;` `    ``// 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++;` `    ``return` `same_dig_cnt;``}` `// Driver code``int` `main()``{` `    ``int` `N = 123, M = 321;` `    ``cout << countDigits(N, M);``    ``return` `0;`

## Java

 `// Java implementation of the above approach``class` `GFG {` `  ``static` `int` `countDigits(``int` `N, ``int` `M) {` `    ``// Convert N and M to string``    ``// for ease of traversal``    ``String a = Integer.toString(N), b = Integer.toString(M);` `    ``int` `same_dig_cnt = ``0``;` `    ``// 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++;` `    ``return` `same_dig_cnt;``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String args[])``  ``{``    ``int` `N = ``123``, M = ``321``;``    ``System.out.println(countDigits(N, M));``  ``}``}` `// This code is contributed by gfgking`

## Python3

 `# Python code for the above approach``def` `countDigits(N, M):` `    ``# Convert N and M to string``    ``# for ease of traversal``    ``a ``=` `str``(N)``    ``b ``=` `str``(M)` `    ``same_dig_cnt ``=` `0``;` `    ``# 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` `    ``return` `same_dig_cnt;` `# Driver code``N ``=` `123``M ``=` `321``;` `print``(countDigits(N, M));` `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# implementation of the above approach``using` `System;``using` `System.Collections;``class` `GFG``{` `  ``static` `int` `countDigits(``int` `N, ``int` `M)``  ``{` `    ``// Convert N and M to string``    ``// for ease of traversal``    ``string` `a = N.ToString(), b = M.ToString();` `    ``int` `same_dig_cnt = 0;` `    ``// 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++;` `    ``return` `same_dig_cnt;``  ``}` `  ``// Driver code``  ``public` `static` `void` `Main()``  ``{` `    ``int` `N = 123, M = 321;` `    ``Console.Write(countDigits(N, M));``  ``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`1`

Time Complexity: O(D), where D is the min 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