Sub-strings of length K containing same character
Last Updated :
06 Sep, 2022
Given a string ‘str’ and an integer ‘k’, the task is to count the number of sub-strings of length ‘k’ which are comprised of the same character. Given string contains only lowercase alphabets.
Examples:
Input: str = "aaaabbbccdddd", k=4
Output: 2
The sub-strings of length 4 which contain identical
characters are 'aaaa' and 'dddd'. So, the count is 2.
Input: str = "aaaaaa", k=4
Output: 3
A simple approach:
- Find all the sub-strings of the string that are of length ‘k’.
- Check if those sub-strings are composed of only ‘1’ character.
- If the above conditions hold then increase the count.
- Display the count.
Efficient approach: We will use Window sliding technique to solve this problem.
- Take a sub-string of length ‘k’ (which is the first ‘k’ characters).
- Then, add next character to the sub-string.
- If the length of the sub-string is greater than ‘k’ then remove the character from the beginning of the string.
- And, count the frequency of the characters in the sub-string with the help of a map.
- If the frequency of one of the sub-string’s character is equal to length of the sub-string itself i.e. all the characters are same.
- Then, increase the count else repeat the steps above until the there’s no more character to add in the end.
- Display the total count in the end.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void solve(string s, int k)
{
int count = 0, length = 0, pos = 0;
map<char, int> m;
for (int i = 0; i < s.length(); i++) {
m[s[i]]++;
length++;
if (length > k) {
m[s[pos++]]--;
length--;
}
if (length == k && m[s[i]] == length)
count++;
}
cout << count << endl;
}
int main()
{
string s = "aaaabbbccdddd";
int k = 4;
solve(s, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void solve(String s, int k)
{
int count = 0, length = 0, pos = 0;
HashMap<Character, Integer> m =
new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); i++)
{
if(m.containsKey(s.charAt(i)))
m.put(s.charAt(i), m.get(s.charAt(i))+1);
else
m.put(s.charAt(i), 1);
length++;
if (length > k)
{
m.put(s.charAt(pos), m.get(s.charAt(pos)) -1 );
pos++;
length--;
}
if (length == k && m.get(s.charAt(i)) == length)
count++;
}
System.out.println( count);
}
public static void main (String[] args)
{
String s = "aaaabbbccdddd";
int k = 4;
solve(s, k);
}
}
|
Python 3
def solve(s, k) :
count, length, pos = 0, 0, 0
m = dict.fromkeys(s,0)
for i in range(len(s)) :
m[s[i]] += 1
length += 1
if length > k :
m[s[pos]] -= 1
pos += 1
length -= 1
if length == k and m[s[i]] == length :
count += 1
print(count)
if __name__ == "__main__" :
s = "aaaabbbccdddd"
k = 4
solve(s, k)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void solve(string s, int k)
{
int count = 0, length = 0, pos = 0;
Dictionary<char, int> m =
new Dictionary<char, int>();
for (int i = 0; i < s.Length; i++) {
if(m.ContainsKey(s[i]))
m[s[i]]++;
else
m[s[i]] = 1;
length++;
if (length > k) {
m[s[pos]]--;
pos++;
length--;
}
if (length == k && m[s[i]] == length)
count++;
}
Console.WriteLine(count);
}
public static void Main () {
string s = "aaaabbbccdddd";
int k = 4;
solve(s, k);
}
}
|
PHP
<?php
function solve($s, $k)
{
$count = 0;
$length = 0;
$pos = 0;
$m = array();
for ($i = 0; $i < strlen($s); $i++)
{
$m[$s[$i]]++;
$length++;
if ($length > $k)
{
$m[$s[$pos++]]--;
$length--;
}
if ($length == $k && $m[$s[$i]] == $length)
$count++;
}
echo $count . "\n";
}
$s = "aaaabbbccdddd";
$k = 4;
solve($s, $k);
?>
|
Javascript
<script>
function solve( s, k)
{
var count = 0, length = 0, pos = 0;
var m = new Map();
for (var i = 0; i < s.length; i++) {
if(!m.has(s[i]))
{
m.set(s[i], 0);
}
m.set(s[i], m.get(s[i])+1);
length++;
if (length > k) {
if(!m.has(s[pos]))
{
m.set(s[pos], 0);
}
m.set(s[pos], m[s[pos]]-1);
pos+=1;
length--;
}
if (length == k && m.get(s[i]) == length)
count++;
}
document.write( count);
}
var s = "aaaabbbccdddd";
var k = 4;
solve(s, k);
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...