Find the count of sub-strings whose characters can be rearranged to form the given word
Last Updated :
10 Nov, 2022
Given a string str, the task is to find the count of all the sub-strings of length four whose characters can be rearranged to form the word “clap”.
Examples:
Input: str = “clapc”
Output: 2
“clap” and “lapc” are the required sub-strings
Input: str = “abcd”
Output: 0
Approach: For every sub-string of length four, count the occurrences of the characters from the word “clap”. If every character has the occurrence exactly one in the sub-string then increment the count. Print the count in the end.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int countOcc(string s)
{
int cnt = 0;
for ( int i = 0; i < s.length() - 3; i++)
{
int c = 0, l = 0, a = 0, p = 0;
for ( int j = i; j < i + 4; j++)
{
switch (s[j])
{
case 'c' :
c++;
break ;
case 'l' :
l++;
break ;
case 'a' :
a++;
break ;
case 'p' :
p++;
break ;
}
}
if (c == 1 && l == 1 && a == 1 && p == 1)
cnt++;
}
return cnt;
}
int main()
{
string s = "clapc" ;
transform(s.begin(), s.end(), s.begin(), :: tolower );
cout << (countOcc(s));
}
|
Java
class GFG {
static int countOcc(String s)
{
int cnt = 0 ;
for ( int i = 0 ; i < s.length() - 3 ; i++) {
int c = 0 , l = 0 , a = 0 , p = 0 ;
for ( int j = i; j < i + 4 ; j++) {
switch (s.charAt(j)) {
case 'c' :
c++;
break ;
case 'l' :
l++;
break ;
case 'a' :
a++;
break ;
case 'p' :
p++;
break ;
}
}
if (c == 1 && l == 1 && a == 1 && p == 1 )
cnt++;
}
return cnt;
}
public static void main(String args[])
{
String s = "clapc" ;
System.out.print(countOcc(s.toLowerCase()));
}
}
|
Python3
def countOcc(s):
cnt = 0
for i in range ( 0 , len (s) - 3 ):
c, l, a, p = 0 , 0 , 0 , 0
for j in range (i, i + 4 ):
if s[j] = = 'c' :
c + = 1
elif s[j] = = 'l' :
l + = 1
elif s[j] = = 'a' :
a + = 1
elif s[j] = = 'p' :
p + = 1
if c = = 1 and l = = 1 and a = = 1 and p = = 1 :
cnt + = 1
return cnt
if __name__ = = "__main__" :
s = "clapc"
print (countOcc(s.lower()))
|
C#
using System;
class GFG
{
static int countOcc( string s)
{
int cnt = 0;
for ( int i = 0; i < s.Length - 3; i++)
{
int c = 0, l = 0, a = 0, p = 0;
for ( int j = i; j < i + 4; j++)
{
switch (s[j])
{
case 'c' :
c++;
break ;
case 'l' :
l++;
break ;
case 'a' :
a++;
break ;
case 'p' :
p++;
break ;
}
}
if (c == 1 && l == 1 && a == 1 && p == 1)
cnt++;
}
return cnt;
}
public static void Main()
{
string s = "clapc" ;
Console.Write(countOcc(s.ToLower()));
}
}
|
PHP
<?php
function countOcc( $s )
{
$cnt = 0;
for ( $i = 0; $i < strlen ( $s ) - 3; $i ++)
{
$c = 0; $l = 0; $a = 0; $p = 0;
for ( $j = $i ; $j < $i + 4; $j ++)
{
switch ( $s [ $j ])
{
case 'c' :
$c ++;
break ;
case 'l' :
$l ++;
break ;
case 'a' :
$a ++;
break ;
case 'p' :
$p ++;
break ;
}
}
if ( $c == 1 && $l == 1 &&
$a == 1 && $p == 1)
$cnt ++;
}
return $cnt ;
}
$s = "clapc" ;
echo countOcc( strtolower ( $s ));
?>
|
Javascript
<script>
function countOcc(s)
{
var cnt = 0;
for ( var i = 0; i < s.length - 3; i++)
{
var c = 0, l = 0, a = 0, p = 0;
for ( var j = i; j < i + 4; j++)
{
switch (s[j])
{
case 'c' :
c++;
break ;
case 'l' :
l++;
break ;
case 'a' :
a++;
break ;
case 'p' :
p++;
break ;
}
}
if (c == 1 && l == 1 && a == 1 && p == 1)
cnt++;
}
return cnt;
}
var s = "clapc" ;
s = s.toLowerCase();
document.write(countOcc(s));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...