Open In App

Print all K digit repeating numbers in a very large number

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++ program for the above approach
#include <bits/stdc++.h>
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<string, int> 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 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<String, Integer> 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<String, Integer> 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 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# 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




<script>
 
// Javascript program for the above approach
 
// Function to print all K digit
// repeating numbers
function print_Kdigit(S, K)
{
 
    // Map to store the substrings
    // with their frequencies
    var m = new Map(); 
 
    // Iterate over every substring
    // and store their frequencies
    // in the map
    for (var i = 0; i < S.length - K;
        i++) {
        var a = S.substring(i,i + K);
 
        // Increment the count of
        // substrings in map
        if(m.has(a))
            m.set(a, m.get(a)+1)
        else
            m.set(a, 1)
    }
 
    // Iterate over all the substrings
    // present in the map
    m.forEach((value, key) => {
     
 
        // Condition to check if the
        // frequency of the substring
        // present in the map
        // is greater than 1
        if (value > 1) {
            document.write(key + " - " +value + "<br>")
        }
    });
}
 
// Driver Code
// Given Number in form of string
var str = "123412345123456";
// Given K
var K = 4;
// Function Call
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


Article Tags :