# Print all K digit repeating numbers in a very large number

• Difficulty Level : Medium
• Last Updated : 27 May, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to print all K digit``// repeating numbers``void` `print_Kdigit(string S, ``int` `K)``{` `    ``// Map to store the substrings``    ``// with their frequencies``    ``map m;` `    ``// Iterate over every substring``    ``// and store their frequencies``    ``// in the map``    ``for` `(``int` `i = 0; i < S.length() - K;``        ``i++) {``        ``string a = S.substr(i, K);` `        ``// Increment the count of``        ``// substrings in map``        ``m[a]++;``    ``}` `    ``// Iterate over all the substrings``    ``// present in the map``    ``for` `(``auto` `x : m) {` `        ``// Condition to check if the``        ``// frequency of the substring``        ``// present in the map``        ``// is greater than 1``        ``if` `(x.second > 1) {``            ``cout << x.first << ``" - "``                ``<< x.second << ``"\n"``;``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given Number in form of string``    ``string str = ``"123412345123456"``;` `    ``// Given K``    ``int` `K = 4;` `    ``// Function Call``    ``print_Kdigit(str, K);``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to print all K digit``// repeating numbers``static` `void` `print_Kdigit(String S, ``int` `K)``{``    ` `    ``// Map to store the substrings``    ``// with their frequencies``    ``Map m = ``new` `HashMap<>();` `    ``// Iterate over every substring``    ``// and store their frequencies``    ``// in the map``    ``for``(``int` `i = ``0``; i < S.length() - K; i++)``    ``{``        ``String a = S.substring(i, i + K);` `        ``// Increment the count of``        ``// substrings in map``        ``m.put(a, m.getOrDefault(a, ``0``) + ``1``);``        ` `    ``}` `    ``// Iterate over all the substrings``    ``// present in the map``    ``for``(Map.Entry x : m.entrySet())``    ``{``        ` `        ``// Condition to check if the``        ``// frequency of the substring``        ``// present in the map``        ``// is greater than 1``        ``if` `(x.getValue() > ``1``)``        ``{``            ``System.out.println(x.getKey() + ``" - "` `+``                            ``x.getValue());``        ``}``    ``}``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given Number in form of string``    ``String str = ``"123412345123456"``;``    ` `    ``// Given K``    ``int` `K = ``4``;``    ` `    ``// Function call``    ``print_Kdigit(str, K);``}``}` `// This code is contributed by offbeat`

## Python3

 `# Python3 program of the above approach``def` `print_Kdigit(S, K):``    ` `    ``# Dictionary to store the substrings``    ``# with their frequencies``    ``m ``=` `{}` `    ``# Iterate over every substring``    ``# and store their frequencies``    ``# in the dictionary``    ``for` `i ``in` `range``(``len``(S) ``-` `K):``        ``a ``=` `S[i:i ``+` `K]``        ` `        ``# Initialize the count of``        ``# substrings in dictionary with 0``        ``m[a] ``=` `0` `    ``for` `i ``in` `range``(``len``(S) ``-` `K):``        ``a ``=` `S[i:i ``+` `K]``        ` `        ``# Increment the count of``        ``# substrings in dictionary``        ``m[a] ``+``=` `1` `    ``# Iterate over all the substrings``    ``# present in the dictionary``    ``for` `key, value ``in` `m.items():``        ``if` `value > ``1``:``            ``print``(key, ``"-"``, value)` `# Driver Code``str` `=` `"123412345123456"` `# Given K``K ``=` `4` `# Function Call``print_Kdigit(``str``, K)` `# This code is contributed by Vishal Maurya`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{` `// Function to print all K digit``// repeating numbers``static` `void` `print_Kdigit(``string` `S, ``int` `K)``{``    ` `    ``// Map to store the substrings``    ``// with their frequencies``    ``Dictionary<``string``,``               ``int``> m = ``new` `Dictionary<``string``,``                                       ``int``>();` `    ``// Iterate over every substring``    ``// and store their frequencies``    ``// in the map``    ``for``(``int` `i = 0; i < S.Length - K; i++)``    ``{``        ``string` `a = S.Substring(i, K);` `        ``// Increment the count of``        ``// substrings in map``        ``m[a] = m.GetValueOrDefault(a, 0) + 1;``    ``}` `    ``// Iterate over all the substrings``    ``// present in the map``    ``foreach``(KeyValuePair<``string``, ``int``> x ``in` `m)``    ``{``        ` `        ``// Condition to check if the``        ``// frequency of the substring``        ``// present in the map``        ``// is greater than 1``        ``if` `(x.Value > 1)``        ``{``            ``Console.Write(x.Key + ``" - "` `+``                          ``x.Value + ``"\n"``);``        ``}``    ``}``}` `// Driver code``public` `static` `void` `Main(``string``[] args)``{``    ` `    ``// Given number in form of string``    ``string` `str = ``"123412345123456"``;``    ` `    ``// Given K``    ``int` `K = 4;``    ` `    ``// Function call``    ``print_Kdigit(str, K);``}``}` `// This code is contributed by rutvik_56`

## Javascript

 ``
Output:
```1234 - 3
2345 - 2```

Time Complexity: O(N*K)

My Personal Notes arrow_drop_up