Print the repeating K digits number in string
Here given code implementation process.
// Java Program
// Print the repeating number of K digits in string
import java.util.HashMap;
public class Repetition
{
// Display all repeating numbers by using of given length k
public void repeatNumber(String text, int k)
{
if (k <= 0)
{
return;
}
// use to collect frequency of every k digit string number
HashMap < String, Integer > record = new HashMap < String, Integer > ();
// Define some auxiliary variables
int i = 0;
boolean result = false;
String temp = "";
// iterate the loop through by length
for (i = 0; i <= text.length() - k; i++)
{
// Get substring
temp = text.substring(i, i + k);
if (record.containsKey(temp))
{
record.put(temp, record.get(temp) + 1);
}
else
{
// Add new k character word
record.put(temp, 1);
}
}
// Display calculated result
for (String word: record.keySet())
{
i = record.get(word);
if (i > 1)
{
// Change the result status
result = true;
// Display k digit number and print its occurrence
System.out.print(word + " Exists " + i + " times \n");
}
}
if (result == false)
{
System.out.print("\n None \n");
}
}
public static void main(String[] args)
{
Repetition task = new Repetition();
// Given text
String text = "15805871111237587415881587123";
// Digit length
int k = 3;
// Test
task.repeatNumber(text, k);
}
}
Output
111 Exists 2 times
871 Exists 2 times
123 Exists 2 times
158 Exists 3 times
587 Exists 3 times
// Include header file
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
// C++ Program
// Print the repeating number of K digits in string
class Repetition
{
public:
// Display all repeating numbers by using of given length k
void repeatNumber(string text, int k)
{
if (k <= 0)
{
return;
}
// use to collect frequency of every k digit string number
unordered_map < string, int > record ;
// Define some auxiliary variables
int i = 0;
bool result = false;
string temp = "";
// iterate the loop through by length
for (i = 0; i <= text.length() - k; i++)
{
// Get substring
temp = text.substr(i, k);
if (record.find(temp) != record.end())
{
record[temp] = record[temp] + 1;
}
else
{
// Add new k character word
record[temp] = 1;
}
}
for (auto &word: record)
{
i = word.second;
if (i > 1)
{
// Change the result status
result = true;
// Display k digit number and print its occurrence
cout << word.first << " Exists " << i << " times \n";
}
}
if (result == false)
{
cout << "\n None \n";
}
}
};
int main()
{
Repetition task = Repetition();
// Given text
string text = "15805871111237587415881587123";
// Digit length
int k = 3;
// Test
task.repeatNumber(text, k);
return 0;
}
Output
111 Exists 2 times
158 Exists 3 times
587 Exists 3 times
123 Exists 2 times
871 Exists 2 times
<?php
// Php Program
// Print the repeating number of K digits in string
class Repetition
{
// Display all repeating numbers by using of given length k
public function repeatNumber($text, $k)
{
if ($k <= 0)
{
return;
}
// use to collect frequency of every k digit string number
$record = array();
// Define some auxiliary variables
$i = 0;
$result = false;
$temp = "";
// iterate the loop through by length
for ($i = 0; $i <= strlen($text) - $k; $i++)
{
// Get substring
$temp = substr($text, $i, $k);
if (array_key_exists($temp, $record))
{
$record[$temp] = $record[$temp] + 1;
}
else
{ // Add new k character word
$record[$temp] = 1;
}
}
foreach($record as $word => $value)
{
$i = $record[$word];
if ($i > 1)
{
// Change the result status
$result = true;
// Display k digit number and print its occurrence
echo $word ." Exists ". $i ." times \n";
}
}
if ($result == false)
{
echo "\n None \n";
}
}
}
function main()
{
$task = new Repetition();
// Given text
$text = "15805871111237587415881587123";
// Digit length
$k = 3;
$task->repeatNumber($text, $k);
}
main();
Output
158 Exists 3 times
587 Exists 3 times
871 Exists 2 times
111 Exists 2 times
123 Exists 2 times
// Node Js Program
// Print the repeating number of K digits in string
class Repetition
{
// Display all repeating numbers by using of given length k
repeatNumber(text, k)
{
if (k <= 0)
{
return;
}
// use to collect frequency of every k digit string number
var record = new Map();
// Define some auxiliary variables
var i = 0;
var result = false;
var temp = "";
// iterate the loop through by length
for (i = 0; i <= text.length - k; i++)
{
// Get substring
temp = text.substring(i, i + k);
if (record.has(temp))
{
record.set(temp, record.get(temp) + 1);
}
else
{
// Add new k character word
record.set(temp, 1);
}
}
for (let [word, value] of record)
{
i = record.get(word);
if (i > 1)
{
// Change the result status
result = true;
// Display k digit number and print its occurrence
process.stdout.write(word + " Exists " + i + " times \n");
}
}
if (result == false)
{
process.stdout.write("\n None \n");
}
}
}
function main()
{
var task = new Repetition();
// Given text
var text = "15805871111237587415881587123";
// Digit length
var k = 3;
// Test
task.repeatNumber(text, k);
}
main();
Output
158 Exists 3 times
587 Exists 3 times
871 Exists 2 times
111 Exists 2 times
123 Exists 2 times
// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Print the repeating number of K digits in string
public class Repetition
{
// Display all repeating numbers by using of given length k
public void repeatNumber(String text, int k)
{
if (k <= 0)
{
return;
}
// use to collect frequency of every k digit string number
Dictionary < string, int > record = new Dictionary < string, int > ();
// Define some auxiliary variables
int i = 0;
Boolean result = false;
string temp = "";
// iterate the loop through by length
for (i = 0; i <= text.Length - k; i++)
{
// Get substring
temp = text.Substring(i, k);
if (record.ContainsKey(temp))
{
record[temp] = record[temp] + 1;
}
else
{
// Add new k character word
record.Add(temp, 1);
}
}
foreach(KeyValuePair < string, int > word in record)
{
i = word.Value;
if (i > 1)
{
// Change the result status
result = true;
// Display k digit number and print its occurrence
Console.Write(word.Key + " Exists " + i + " times \n");
}
}
if (result == false)
{
Console.Write("\n None \n");
}
}
public static void Main(string[] args)
{
Repetition task = new Repetition();
// Given text
String text = "15805871111237587415881587123";
// Digit length
int k = 3;
// Test
task.repeatNumber(text, k);
}
}
Output
158 Exists 3 times
587 Exists 3 times
871 Exists 2 times
111 Exists 2 times
123 Exists 2 times
# Python 3 Program
# Print the repeating number of K digits in string
class Repetition :
# Display all repeating numbers by using of given length k
def repeatNumber(self, text, k) :
if (k <= 0) :
return
# use to collect frequency of every k digit string number
record = dict()
# Define some auxiliary variables
i = 0
result = False
temp = ""
# iterate the loop through by length
while (i <= len(text) - k) :
# Get substring
temp = text[i: i + k]
if (temp in record.keys()) :
record[temp] = record.get(temp) + 1
else :
# Add new k character word
record[temp] = 1
i += 1
for word, value in record.items() :
i = value
if (i > 1) :
# Change the result status
result = True
# Display k digit number and print its occurrence
print(word ,"Exists", i ,"times")
if (result == False) :
print("\n None ")
def main() :
task = Repetition()
# Given text
text = "15805871111237587415881587123"
# Digit length
k = 3
# Test
task.repeatNumber(text, k)
if __name__ == "__main__": main()
Output
587 Exists 3 times
111 Exists 2 times
123 Exists 2 times
871 Exists 2 times
158 Exists 3 times
# Ruby Program
# Print the repeating number of K digits in string
class Repetition
# Display all repeating numbers by using of given length k
def repeatNumber(text, k)
if (k <= 0)
return
end
# use to collect frequency of every k digit string number
record = Hash.new
# Define some auxiliary variables
i = 0
result = false
temp = ""
# iterate the loop through by length
while (i <= text.length - k)
# Get substring
temp = text[i...i + k]
if (record.key?(temp))
record[temp] = record[temp] + 1
else
record[temp] = 1
end
i += 1
end
record.each { | word, value |
i = value
if (i > 1)
# Change the result status
result = true
# Display k digit number and print its occurrence
print(word ," Exists ", i ," times \n")
end
}
if (result == false)
print("\n None \n")
end
end
end
def main()
task = Repetition.new()
# Given text
text = "15805871111237587415881587123"
# Digit length
k = 3
# Test
task.repeatNumber(text, k)
end
main()
Output
158 Exists 3 times
587 Exists 3 times
871 Exists 2 times
111 Exists 2 times
123 Exists 2 times
import scala.collection.mutable._;
// Scala Program
// Print the repeating number of K digits in string
class Repetition
{
// Display all repeating numbers by using of given length k
def repeatNumber(text: String, k: Int): Unit = {
if (k <= 0)
{
return;
}
// use to collect frequency of every k digit string number
var record = Map[String, Int]();
// Define some auxiliary variables
var i: Int = 0;
var result: Boolean = false;
var temp: String = "";
// iterate the loop through by length
while (i <= text.length() - k)
{
// Get substring
temp = text.substring(i, i + k);
if (record.contains(temp))
{
record.addOne(temp, record.get(temp).get + 1);
}
else
{
// Add new k character word
record.addOne(temp, 1);
}
i += 1;
}
for ((key, v) <- record)
{
i = v;
if (i > 1)
{
// Change the result status
result = true;
// Display k digit number and print its occurrence
print(key + " Exists " + i + " times \n");
}
}
if (result == false)
{
print("\n None \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Repetition = new Repetition();
// Given text
var text: String = "15805871111237587415881587123";
// Digit length
var k: Int = 3;
// Test
task.repeatNumber(text, k);
}
}
Output
111 Exists 2 times
123 Exists 2 times
871 Exists 2 times
158 Exists 3 times
587 Exists 3 times
import Foundation
// Swift 4 Program
// Print the repeating number of K digits in string
class Repetition
{
// Display all repeating numbers by using of given length k
func repeatNumber(_ text: String, _ k: Int)
{
if (k <= 0)
{
return;
}
// use to collect frequency of every k digit string number
var record = [String: Int]();
// Define some auxiliary variables
var i: Int = 0;
var result: Bool = false;
var temp: String = "";
// iterate the loop through by length
while (i <= text.count - k)
{
// find index of substring
let start = text.index(text.startIndex, offsetBy: i)
let end = text.index(text.endIndex, offsetBy: -(text.count-(i+k)))
// Get substring
temp = String(text[start..<end]);
if (record.keys.contains(temp))
{
record[temp] = record[temp]! + 1;
}
else
{
// Add new k character word
record[temp] = 1;
}
i += 1;
}
for (key, value) in record
{
i = value;
if (i > 1)
{
// Change the result status
result = true;
// Display k digit number and print its occurrence
print(key ," Exists", i ,"times");
}
}
if (result == false)
{
print("\n None ");
}
}
}
func main()
{
let task: Repetition = Repetition();
// Given text
let text: String = "15805871111237587415881587123";
// Digit length
let k: Int = 3;
// Test
task.repeatNumber(text, k);
}
main();
Output
158 Exists 3 times
123 Exists 2 times
111 Exists 2 times
587 Exists 3 times
871 Exists 2 times
// Kotlin Program
// Print the repeating number of K digits in string
class Repetition
{
// Display all repeating numbers by using of given length k
fun repeatNumber(text: String, k: Int): Unit
{
if (k <= 0)
{
return;
}
// use to collect frequency of every k digit string number
var record = mutableMapOf < String , Int > ();
// Define some auxiliary variables
var i: Int = 0;
var result: Boolean = false;
var temp: String;
// iterate the loop through by length
while (i <= text.length - k)
{
// Get substring
temp = text.subSequence(i, i + k).toString();
if (record.containsKey(temp))
{
record.put(temp, record.getValue(temp) + 1);
}
else
{
// Add new k character word
record.put(temp, 1);
}
i += 1;
}
// Display calculated result
for (word in record.keys)
{
i = record.getValue(word);
if (i > 1)
{
// Change the result status
result = true;
// Display k digit number and print its occurrence
print(word + " Exists " + i + " times \n");
}
}
if (result == false)
{
print("\n None \n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Repetition = Repetition();
// Given text
var text: String = "15805871111237587415881587123";
// Digit length
var k: Int = 3;
// Test
task.repeatNumber(text, k);
}
Output
158 Exists 3 times
587 Exists 3 times
871 Exists 2 times
111 Exists 2 times
123 Exists 2 times

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment