Posted on by Kalkicode
Code Hash

# Count of substrings of length K with exactly K distinct characters

Here given code implementation process.

``````// Java Program
// Count of substrings of length K with exactly K distinct characters
import java.util.HashMap;
public class Palindrome
{
// Count all distinct k character of k length
public void countDistinctK(String text, int k)
{
int count = 0;
// Display calculated result
System.out.print(" Given Text : " + text);
System.out.print("\n K : " + k);
if (k < text.length())
{
// Define loop controlling variable i
int i = 0;
// Use to count character frequency
HashMap < Character, Integer > record = new HashMap < Character, Integer > ();
// Find the initials K character frequency
for (i = 0; i < k; i++)
{
if (record.containsKey(text.charAt(i)))
{
// increase character frequency
record.put(text.charAt(i), record.get(text.charAt(i)) + 1);
}
else
{
record.put(text.charAt(i), 1);
}
}
if (record.size() == k)
{
System.out.print("\n(" + text.substring(0, k) + ")");
// Initials character are distinct
count = 1;
}
for (i = k; i < text.length(); i++)
{
if (record.containsKey(text.charAt(i)))
{
// increase character frequency
record.put(text.charAt(i), record.get(text.charAt(i)) + 1);
}
else
{
record.put(text.charAt(i), 1);
}
if (record.get(text.charAt(i - k)) > 1)
{
// Reduce the (i - k) - th character frequency
// Reduce character frequency
record.put(text.charAt(i - k), record.get(text.charAt(i - k)) - 1);
}
else
{
// When (i - k) - th character frequency is one then remove
record.remove(text.charAt(i - k));
}
if (record.size() == k)
{
System.out.print("\n(" + text.substring(i - k + 1, i + 1) + ")");
// When record size equal to k
// increase resultant counter
count++;
}
}
}
System.out.print("\n Result : " + count);
}
public static void main(String[] args)
{
String text = "KFGTFTITCSGRROUPT";
int k = 4;
// KFGTFTITCSGRROUP
// All K characters resultant pair is
// ((KFGT)
// (ITCS)
// (TCSG)
// (CSGR)
// (ROUP)
// (OUPT)
}
}``````

#### Output

`````` Given Text : KFGTFTITCSGRROUPT
K : 4
(KFGT)
(ITCS)
(TCSG)
(CSGR)
(ROUP)
(OUPT)
Result : 6``````
``````// Include header file
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

// C++ Program
// Count of substrings of length K with exactly K distinct characters

class Palindrome
{
public:
// Count all distinct k character of k length
void countDistinctK(string text, int k)
{
int count = 0;
// Display calculated result
cout << " Given Text : " << text;
cout << "\n K : " << k;
if (k < text.length())
{
// Define loop controlling variable i
int i = 0;
// Use to count character frequency
unordered_map < char, int > record ;
// Find the initials K character frequency
for (i = 0; i < k; i++)
{
if (record.find(text[i]) != record.end())
{
// increase character frequency
record[text[i]] = record[text[i]] + 1;
}
else
{
record[text[i]] = 1;
}
}
if (record.size() == k)
{
cout << "\n(" << text.substr(0, k) << ")";
// Initials character are distinct
count = 1;
}
for (i = k; i < text.length(); i++)
{
if (record.find(text[i]) != record.end())
{
// increase character frequency
record[text[i]] = record[text[i]] + 1;
}
else
{
record[text[i]] = 1;
}
if (record[text[i - k]] > 1)
{
// Reduce the (i - k) - th character frequency
// Reduce character frequency
record[text[i - k]] = record[text[i - k]] - 1;
}
else
{
// When (i - k) - th character frequency is one then remove
record.erase(text[i - k]);
}
if (record.size() == k)
{
// When record size equal to k
// increase resultant counter
cout << "\n(" << text.substr(i - k + 1, k) << ")";
count++;
}
}
}
cout << "\n Result : " << count;
}
};
int main()
{
string text = "KFGTFTITCSGRROUPT";
int k = 4;
// KFGTFTITCSGRROUP
// All K characters resultant pair is
// ((KFGT)
// (ITCS)
// (TCSG)
// (CSGR)
// (ROUP)
// (OUPT)
return 0;
}``````

#### Output

`````` Given Text : KFGTFTITCSGRROUPT
K : 4
(KFGT)
(ITCS)
(TCSG)
(CSGR)
(ROUP)
(OUPT)
Result : 6``````
``````// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Count of substrings of length K with exactly K distinct characters
public class Palindrome
{
// Count all distinct k character of k length
public void countDistinctK(String text, int k)
{
int count = 0;
// Display calculated result
Console.Write(" Given Text : " + text);
Console.Write("\n K : " + k);
if (k < text.Length)
{
// Define loop controlling variable i
int i = 0;
// Use to count character frequency
Dictionary < char, int > record = new Dictionary < char, int > ();
// Find the initials K character frequency
for (i = 0; i < k; i++)
{
if (record.ContainsKey(text[i]))
{
// increase character frequency
}
else
{
}
}
if (record.Count == k)
{
Console.Write("\n(" + text.Substring(0, k) + ")");
// Initials character are distinct
count = 1;
}
for (i = k; i < text.Length; i++)
{
if (record.ContainsKey(text[i]))
{
// increase character frequency
record[text[i]] = record[text[i]] + 1;
}
else
{
}
if (record[text[i - k]] > 1)
{
// Reduce the (i - k) - th character frequency
// Reduce character frequency
record[text[i - k]] =  record[text[i - k]] - 1;
}
else
{
// When (i - k) - th character frequency is one then remove
record.Remove(text[i - k]);
}
if (record.Count == k)
{
// When record size equal to k
// increase resultant counter
Console.Write("\n(" + text.Substring(i - k + 1, k) + ")");
count++;
}
}
}
Console.Write("\n Result : " + count);
}
public static void Main(String[] args)
{
String text = "KFGTFTITCSGRROUPT";
int k = 4;
// KFGTFTITCSGRROUP
// All K characters resultant pair is
// ((KFGT)
// (ITCS)
// (TCSG)
// (CSGR)
// (ROUP)
// (OUPT)
}
}``````

#### Output

`````` Given Text : KFGTFTITCSGRROUPT
K : 4
(KFGT)
(ITCS)
(TCSG)
(CSGR)
(ROUP)
(OUPT)
Result : 6``````
``````<?php
// Php Program
// Count of substrings of length K with exactly K distinct characters
class Palindrome
{
// Count all distinct k character of k length
public	function countDistinctK(\$text, \$k)
{
\$count = 0;
// Display calculated result
echo " Given Text : ". \$text;
echo "\n K : ". \$k;
if (\$k < strlen(\$text))
{
// Define loop controlling variable i
\$i = 0;
// Use to count character frequency
\$record = array();
// Find the initials K character frequency
for (\$i = 0; \$i < \$k; \$i++)
{
if (array_key_exists(\$text[\$i], \$record))
{ // increase character frequency
\$record[\$text[\$i]] = \$record[\$text[\$i]] + 1;
}
else
\$record[\$text[\$i]] = 1;
}
}
if (count(\$record) == \$k)
{
echo "\n(". substr(\$text, 0, \$k).")";
// Initials character are distinct
\$count = 1;
}
for (\$i = \$k; \$i < strlen(\$text); \$i++)
{
if (array_key_exists(\$text[\$i], \$record))
{ // increase character frequency
\$record[\$text[\$i]] = \$record[\$text[\$i]] + 1;
}
else
\$record[\$text[\$i]] = 1;
}
if (\$record[\$text[\$i - \$k]] > 1)
{ // Reduce the (i - k) - th character frequency
// Reduce character frequency
\$record[\$text[\$i - \$k]] = \$record[\$text[\$i - \$k]] - 1;
}
else
{
unset( // When (i - k) - th character frequency is one then remove
\$record[\$text[\$i - \$k]]);
}
if (count(\$record) == \$k)
{
// When record size equal to k
// increase resultant counter
echo "\n(". substr(\$text, \$i - \$k + 1, \$k) .")";
\$count++;
}
}
}
echo "\n Result : ". \$count;
}
}

function main()
{
\$text = "KFGTFTITCSGRROUPT";
\$k = 4;
}
main();``````

#### Output

`````` Given Text : KFGTFTITCSGRROUPT
K : 4
(KFGT)
(ITCS)
(TCSG)
(CSGR)
(ROUP)
(OUPT)
Result : 6``````
``````// Node Js Program
// Count of substrings of length K with exactly K distinct characters
class Palindrome
{
// Count all distinct k character of k length
countDistinctK(text, k)
{
var count = 0;
// Display calculated result
process.stdout.write(" Given Text : " + text);
process.stdout.write("\n K : " + k);
if (k < text.length)
{
// Define loop controlling variable i
var i = 0;
// Use to count character frequency
var record = new Map();
// Find the initials K character frequency
for (i = 0; i < k; i++)
{
if (record.has(text.charAt(i)))
{
// increase character frequency
record.set(text.charAt(i), record.get(text.charAt(i)) + 1);
}
else
{
record.set(text.charAt(i), 1);
}
}
if (record.size == k)
{
process.stdout.write("\n(" + text.substring(0, k) + ")");
// Initials character are distinct
count = 1;
}
for (i = k; i < text.length; i++)
{
if (record.has(text.charAt(i)))
{
// increase character frequency
record.set(text.charAt(i), record.get(text.charAt(i)) + 1);
}
else
{
record.set(text.charAt(i), 1);
}
if (record.get(text.charAt(i - k)) > 1)
{
// Reduce the (i - k) - th character frequency
// Reduce character frequency
record.set(text.charAt(i - k), record.get(text.charAt(i - k)) - 1);
}
else
{
// When (i - k) - th character frequency is one then remove
record.delete(text.charAt(i - k));
}
if (record.size == k)
{
// When record size equal to k
// increase resultant counter
process.stdout.write("\n(" + text.substring(i - k + 1, i + 1) + ")");
count++;
}
}
}
process.stdout.write("\n Result : " + count);
}
}

function main()
{
var text = "KFGTFTITCSGRROUPT";
var k = 4;
// KFGTFTITCSGRROUP
// All K characters resultant pair is
// ((KFGT)
// (ITCS)
// (TCSG)
// (CSGR)
// (ROUP)
// (OUPT)
}
main();``````

#### Output

`````` Given Text : KFGTFTITCSGRROUPT
K : 4
(KFGT)
(ITCS)
(TCSG)
(CSGR)
(ROUP)
(OUPT)
Result : 6``````
``````#  Python 3 Program
#  Count of substrings of length K with exactly K distinct characters
class Palindrome :
#  Count all distinct k character of k length
def countDistinctK(self, text, k) :
count = 0
#  Display calculated result
print(" Given Text : ", text, end = "")
print("\n K : ", k, end = "")
if (k < len(text)) :
#  Define loop controlling variable i
i = 0
#  Use to count character frequency
record = dict()
#  Find the initials K character frequency
while (i < k) :
if (text[i] in record.keys()) :
#  increase character frequency
record[text[i]] = record.get(text[i]) + 1
else :
record[text[i]] = 1

i += 1

if (len(record) == k) :
print("\n(", text[0: k] ,")", end = "")
#  Initials character are distinct
count = 1

i = k
while (i < len(text)) :
if (text[i] in record.keys()) :
#  increase character frequency
record[text[i]] = record.get(text[i]) + 1
else :
record[text[i]] = 1

if (record.get(text[i - k]) > 1) :
#  Reduce the (i - k) - th character frequency
#  Reduce character frequency
record[text[i - k]] = record.get(text[i - k]) - 1
else :
#  When (i - k) - th character frequency is one then remove
del record[text[i - k]]

if (len(record) == k) :
#  When record size equal to k
#  increase resultant counter
print("\n(", text[i - k + 1: i + 1] ,")", end = "")
count += 1

i += 1

print("\n Result : ", count, end = "")

def main() :
text = "KFGTFTITCSGRROUPT"
k = 4
#  KFGTFTITCSGRROUP
#  All K characters resultant pair is
#  ((KFGT)
#  (ITCS)
#  (TCSG)
#  (CSGR)
#  (ROUP)
#  (OUPT)

if __name__ == "__main__": main()``````

#### Output

`````` Given Text :  KFGTFTITCSGRROUPT
K :  4
( KFGT )
( ITCS )
( TCSG )
( CSGR )
( ROUP )
( OUPT )
Result :  6``````
``````#  Ruby Program
#  Count of substrings of length K with exactly K distinct characters
class Palindrome
#  Count all distinct k character of k length
def countDistinctK(text, k)
count = 0
#  Display calculated result
print(" Given Text : ", text)
print("\n K : ", k)
if (k < text.length)
#  Define loop controlling variable i
i = 0
#  Use to count character frequency
record = Hash.new
#  Find the initials K character frequency
while (i < k)
if (record.key?(text[i]))
record[text[i]] = record[text[i]] + 1
else
record[text[i]] = 1
end

i += 1
end

if (record.size() == k)
print("\n(", text[0...k] ,")")
#  Initials character are distinct
count = 1
end

i = k
while (i < text.length)
if (record.key?(text[i]))
record[text[i]] = record[text[i]] + 1
else
record[text[i]] = 1
end

if (record[text[i - k]] > 1)
record[text[i - k]] = record[text[i - k]] - 1
else
record.delete(text[i - k])
end

if (record.size() == k)
#  When record size equal to k
#  increase resultant counter
print("\n(", text[i - k + 1...i + 1] ,")")
count += 1
end

i += 1
end

end

print("\n Result : ", count)
end

end

def main()
text = "KFGTFTITCSGRROUPT"
k = 4
#  KFGTFTITCSGRROUP
#  All K characters resultant pair is
#  ((KFGT)
#  (ITCS)
#  (TCSG)
#  (CSGR)
#  (ROUP)
#  (OUPT)
end

main()``````

#### Output

`````` Given Text : KFGTFTITCSGRROUPT
K : 4
(KFGT)
(ITCS)
(TCSG)
(CSGR)
(ROUP)
(OUPT)
Result : 6``````
``````import scala.collection.mutable._;
// Scala Program
// Count of substrings of length K with exactly K distinct characters
class Palindrome
{
// Count all distinct k character of k length
def countDistinctK(text: String, k: Int): Unit = {
var count: Int = 0;
// Display calculated result
print(" Given Text : " + text);
print("\n K : " + k);
if (k < text.length())
{
// Define loop controlling variable i
var i: Int = 0;
// Use to count character frequency
var record  = Map[Character, Int]();
// Find the initials K character frequency
while (i < k)
{
if (record.contains(text.charAt(i)))
{
// increase character frequency
}
else
{
}
i += 1;
}
if (record.size == k)
{
print("\n(" + text.substring(0, k) + ")");
// Initials character are distinct
count = 1;
}
i = k;
while (i < text.length())
{
if (record.contains(text.charAt(i)))
{
// increase character frequency
}
else
{
}
if (record.get(text.charAt(i - k)).get > 1)
{
// Reduce the (i - k) - th character frequency
// Reduce character frequency
record.addOne(text.charAt(i - k), record.get(text.charAt(i - k)).get - 1);
}
else
{
// When (i - k) - th character frequency is one then remove
record.remove(text.charAt(i - k));
}
if (record.size == k)
{
// When record size equal to k
// increase resultant counter
print("\n(" + text.substring(i - k + 1, i + 1) + ")");
count += 1;
}
i += 1;
}
}
print("\n Result : " + count);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Palindrome = new Palindrome();
var text: String = "KFGTFTITCSGRROUPT";
var k: Int = 4;
// KFGTFTITCSGRROUP
// All K characters resultant pair is
// ((KFGT)
// (ITCS)
// (TCSG)
// (CSGR)
// (ROUP)
// (OUPT)
}
}``````

#### Output

`````` Given Text : KFGTFTITCSGRROUPT
K : 4
(KFGT)
(ITCS)
(TCSG)
(CSGR)
(ROUP)
(OUPT)
Result : 6``````
``````// Kotlin Program
// Count of substrings of length K with exactly K distinct characters
class Palindrome
{
// Count all distinct k character of k length
fun countDistinctK(text: String, k: Int): Unit
{
var count: Int = 0;
// Display calculated result
print(" Given Text : " + text);
print("\n K : " + k);
if (k < text.length)
{
// Define loop controlling variable i
var i: Int = 0;
// Use to count character frequency
var record = mutableMapOf < Char , Int > ();
// Find the initials K character frequency
while (i < k)
{
if (record.containsKey(text.get(i)))
{
// increase character frequency
record.put(text.get(i), record.getValue(text.get(i)) + 1);
}
else
{
record.put(text.get(i), 1);
}
i += 1;
}
if (record.count() == k)
{
print("\n(" + text.subSequence(0, k) + ")").toString();
// Initials character are distinct
count = 1;
}
i = k;
while (i < text.length)
{
if (record.containsKey(text.get(i)))
{
// increase character frequency
record.put(text.get(i), record.getValue(text.get(i)) + 1);
}
else
{
record.put(text.get(i), 1);
}
if (record.getValue(text.get(i - k)) > 1)
{
// Reduce the (i - k) - th character frequency
// Reduce character frequency
record.put(text.get(i - k), record.getValue(text.get(i - k)) - 1);
}
else
{
// When (i - k) - th character frequency is one then remove
record.remove(text.get(i - k));
}
if (record.count() == k)
{
// When record size equal to k
// increase resultant counter
print("\n(" + text.subSequence(i - k + 1, i + 1).toString() + ")");
count += 1;
}
i += 1;
}
}
print("\n Result : " + count);
}
}
fun main(args: Array < String > ): Unit
{
var text: String = "KFGTFTITCSGRROUPT";
var k: Int = 4;
// KFGTFTITCSGRROUP
// All K characters resultant pair is
// ((KFGT)
// (ITCS)
// (TCSG)
// (CSGR)
// (ROUP)
// (OUPT)
}``````

#### Output

`````` Given Text : KFGTFTITCSGRROUPT
K : 4
(KFGT)
(ITCS)
(TCSG)
(CSGR)
(ROUP)
(OUPT)
Result : 6``````
``````import Foundation
// Swift 4 Program
// Count of substrings of length K with exactly K distinct characters
class Palindrome
{
// Count all distinct k character of k length
func countDistinctK(_ t: String, _ k: Int)
{
var text = Array(t);
var count: Int = 0;
// Display calculated result
print(" Given Text : ", t, terminator: "");
print("\n K : ", k, terminator: "");
if (k < text.count)
{
// Define loop controlling variable i
var i: Int = 0;
// Use to count character frequency
var record = [Character: Int]();
// Find the initials K character frequency
while (i < k)
{
if (record.keys.contains(text[i]))
{
// increase character frequency
record[text[i]] = record[text[i]]! + 1;
}
else
{
record[text[i]] = 1;
}
i += 1;
}
if (record.count == k)
{
// find index of substring

print("\n(", String(text.prefix(k)) ,")", terminator: "");
// Initials character are distinct
count = 1;
}
i = k;
while (i < text.count)
{
if (record.keys.contains(text[i]))
{
// increase character frequency
record[text[i]] = record[text[i]]! + 1;
}
else
{
record[text[i]] = 1;
}
if (record[text[i - k]]! > 1)
{
// Reduce the (i - k) - th character frequency
// Reduce character frequency
record[text[i - k]] = record[text[i - k]]! - 1;
}
else
{
// When (i - k) - th character frequency is one then remove
record.removeValue(forKey: text[i - k]);
}
if (record.count == k)
{
// find index of substring
let range = (i - k + 1)..<( i + 1);
// When record size equal to k
// increase resultant counter
print("\n(", String(text[range]) ,")", terminator: "");
count += 1;
}
i += 1;
}
}
print("\n Result : ", count, terminator: "");
}
}
func main()
{
let text: String = "KFGTFTITCSGRROUPT";
let k: Int = 4;
// KFGTFTITCSGRROUP
// All K characters resultant pair is
// ((KFGT)
// (ITCS)
// (TCSG)
// (CSGR)
// (ROUP)
// (OUPT)
}
main();``````

#### Output

`````` Given Text :  KFGTFTITCSGRROUPT
K :  4
( KFGT )
( ITCS )
( TCSG )
( CSGR )
( ROUP )
( OUPT )
Result :  6``````

## Comment

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.

Categories
Relative Post