Print all K digit repeating numbers in a very large number
Last Updated :
18 Jul, 2024
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);
Time Complexity: O(N*K)
Space Complexity: O(N) //N is the length of the string
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
Print all 3 digit repeating numbers in a very large number Given a very large number, print all the 3 digit repeating numbers with their frequency. If a 3 digit number appears more than once, print the number and its frequency. Example: Input: 123412345123456 Output: 123 - 3 times 234 - 3 times 345 - 2 times Input: 43243243 Output: 432 - 2 times 324 - 2 tim
6 min read
Count of N-digit numbers with at least one digit repeating Given a positive integer n, the task is to find the count of n-digit numbers with at least one repeated digit.Examples:Input: n = 2Output: 9Explanation: All the 2-digit numbers with at least one repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99. Therefore, the total count is 9.Input: n = 5Output
15+ min read
Count of repeating digits in a given Number Given a number N, the task is to count the total number of repeating digits in the given number. Examples: Input: N = 99677 Output: 2Explanation:In the given number only 9 and 7 are repeating, hence the answer is 2. Input: N = 12Output: 0Explanation:In the given number no digits are repeating, hence
12 min read
Print a number strictly less than a given number such that all its digits are distinct. Given a positive number n, print a number less than n such that all its digits are distinct.Examples: Input : 1134 Output : 1098 1098 is the largest number smaller than 1134 such that all digits are distinct. Input : 4559 Output : 4539 The problem can easily be solved by using counting. Firstly, loo
6 min read
Program that receives a number and prints it out in large size Write a program that receives a number n as input and prints it in large size on a same line as shown below in the image. Examples :Â Input : n = 0194Output : Input : n = 0123456789Output : We use two-dimensional character array to store those hash strings for each digit. The for loop reads each dig
15+ min read