Open In App

Print all K digit repeating numbers in a very large number

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// 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); 

    return 0;
} 
//The code is updated by Sahil Srivastava (gfg_ian0001
Java
import java.util.HashMap;
import java.util.Map;

public class KDigitRepeatingNumbers {

    // Function to print all K digit repeating numbers
    public static void printKDigit(String S, int K) {
        // Map to store the substrings with their frequencies
        Map<String, Integer> map = 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
            map.put(a, map.getOrDefault(a, 0) + 1);
        }

        // Iterate over all the substrings present in the map
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            // Condition to check if the frequency of the substring present in the map is greater than 1
            if (entry.getValue() > 1) {
                System.out.println(entry.getKey() + " - " + entry.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
        printKDigit(str, K);
    }
}
Python
def print_k_digit(s, k):
    # Dictionary to store the substrings with their frequencies
    substr_freq = {}

    # Iterate over every substring and store their frequencies in the dictionary
    for i in range(len(s) - k + 1):
        substr = s[i:i + k]

        # Increment the count of substrings in dictionary
        if substr in substr_freq:
            substr_freq[substr] += 1
        else:
            substr_freq[substr] = 1

    # Iterate over all the substrings present in the dictionary
    for substr, freq in substr_freq.items():
        # Condition to check if the frequency of the substring present in the dictionary is greater than 1
        if freq > 1:
            print(f"{substr} - {freq}")

# Driver Code
if __name__ == "__main__":
    # Given Number in form of string
    str_num = "123412345123456"

    # Given K
    K = 4

    # Function Call
    print_k_digit(str_num, K)
C#
using System;
using System.Collections.Generic;

class KDigitRepeatingNumbers
{
    // Function to print all K digit repeating numbers
    static void PrintKDigit(string s, int k)
    {
        // Dictionary to store the substrings with their frequencies
        Dictionary<string, int> map = 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 substr = s.Substring(i, k);

            // Increment the count of substrings in map
            if (map.ContainsKey(substr))
            {
                map[substr]++;
            }
            else
            {
                map[substr] = 1;
            }
        }

        // Iterate over all the substrings present in the map
        foreach (var entry in map)
        {
            // Condition to check if the frequency of the substring present in the map is greater than 1
            if (entry.Value > 1)
            {
                Console.WriteLine($"{entry.Key} - {entry.Value}");
            }
        }
    }

    // Driver Code
    static void Main()
    {
        // Given Number in form of string
        string str = "123412345123456";

        // Given K
        int K = 4;

        // Function Call
        PrintKDigit(str, K);
    }
}
Javascript
function printKDigit(s, k) {
    // Object to store the substrings with their frequencies
    const substrFreq = {};

    // Iterate over every substring and store their frequencies in the object
    for (let i = 0; i <= s.length - k; i++) {
        const substr = s.substring(i, i + k);

        // Increment the count of substrings in object
        if (substr in substrFreq) {
            substrFreq[substr]++;
        } else {
            substrFreq[substr] = 1;
        }
    }

    // Iterate over all the substrings present in the object
    for (const [substr, freq] of Object.entries(substrFreq)) {
        // Condition to check if the frequency of the substring present in the object is greater than 1
        if (freq > 1) {
            console.log(`${substr} - ${freq}`);
        }
    }
}

// Driver Code
const str = "123412345123456";
const K = 4;

// Function Call
printKDigit(str, K);

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


Next Article

Similar Reads