Given a very large number N in the form of a string and a number K, the task is to print all the K-digit repeating numbers whose frequency is greater than 1.
Examples:
Input: str = “123412345123456”, K = 4
Output:
1234 – 3
2345 – 2
Explanation:
The 4-digit numbers having frequency greater than 1 are 1234 and 2345.
Input: N = 1432543214325432, K = 5
Output:
14325 – 2
32543 – 2
43254 – 2
Explanation:
The 5-digit numbers having frequency greater than 1 are 14325, 32543, and 43254.
Approach: Since the number is given in the form of a string, the idea is to store all the substring of size K in a map with their frequency. Now, while iterating the Map, it prints only those substrings which have a frequency greater than one along with the number of times they appear.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void print_Kdigit(string S, int K)
{
map<string, int > m;
for ( int i = 0; i < S.length() - K;
i++) {
string a = S.substr(i, K);
m[a]++;
}
for ( auto x : m) {
if (x.second > 1) {
cout << x.first << " - "
<< x.second << "\n" ;
}
}
}
int main()
{
string str = "123412345123456" ;
int K = 4;
print_Kdigit(str, K);
}
|
Java
import java.util.*;
class GFG{
static void print_Kdigit(String S, int K)
{
Map<String, Integer> m = new HashMap<>();
for ( int i = 0 ; i < S.length() - K; i++)
{
String a = S.substring(i, i + K);
m.put(a, m.getOrDefault(a, 0 ) + 1 );
}
for (Map.Entry<String, Integer> x : m.entrySet())
{
if (x.getValue() > 1 )
{
System.out.println(x.getKey() + " - " +
x.getValue());
}
}
}
public static void main(String[] args)
{
String str = "123412345123456" ;
int K = 4 ;
print_Kdigit(str, K);
}
}
|
Python3
def print_Kdigit(S, K):
m = {}
for i in range ( len (S) - K):
a = S[i:i + K]
m[a] = 0
for i in range ( len (S) - K):
a = S[i:i + K]
m[a] + = 1
for key, value in m.items():
if value > 1 :
print (key, "-" , value)
str = "123412345123456"
K = 4
print_Kdigit( str , K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void print_Kdigit( string S, int K)
{
Dictionary< string ,
int > m = new Dictionary< string ,
int >();
for ( int i = 0; i < S.Length - K; i++)
{
string a = S.Substring(i, K);
m[a] = m.GetValueOrDefault(a, 0) + 1;
}
foreach (KeyValuePair< string , int > x in m)
{
if (x.Value > 1)
{
Console.Write(x.Key + " - " +
x.Value + "\n" );
}
}
}
public static void Main( string [] args)
{
string str = "123412345123456" ;
int K = 4;
print_Kdigit(str, K);
}
}
|
Javascript
<script>
function print_Kdigit(S, K)
{
var m = new Map();
for ( var i = 0; i < S.length - K;
i++) {
var a = S.substring(i,i + K);
if (m.has(a))
m.set(a, m.get(a)+1)
else
m.set(a, 1)
}
m.forEach((value, key) => {
if (value > 1) {
document.write(key + " - " +value + "<br>" )
}
});
}
var str = "123412345123456" ;
var K = 4;
print_Kdigit(str, K);
</script>
|
Output: 1234 - 3
2345 - 2
Time Complexity: O(N*K)
Space Complexity: O(N) //N is the length of the string
Related Topic: Subarrays, Subsequences, and Subsets in Array