Number of ways in which the substring in range [L, R] can be formed using characters out of the range
Given a string S and a range [L, R]. The task is to find the number of ways in which the sub-string in the range S[L, R] can be constructed using the characters that exist in the string but do not lie in the range S[L, R].
Examples:
Input: s = “cabcaab”, l = 1, r = 3
Output: 2
The substring is “abc”
s[4] + s[6] + s[0] = ‘a’ + ‘b’ + ‘c’ = “abc”
s[5] + s[6] + s[0] = ‘a’ + ‘b’ + ‘c’ = “abc”
Input: s = “aaaa”, l = 1, r = 2
Output: 2
Approach: The problem can be solved using hash-table and combinatorics. The following steps can be followed to solve the above problem:
- Count the frequency of every character that does not lie in the range L and R in the hash-table(say freq).
- Iterate from L to R separately and calculate the number of ways.
- For every character in range L and R, the number of ways is multiplied by freq[s[i]-‘a’] and decreases the value of freq[s[i]-‘a’] by 1.
- In case the freq[s[i]-‘a’] value is 0, we do not have any characters to fill up that place, hence the number of ways will be 0.
- In the end, the overall multiplication will be our answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int calculateWays(string s, int n, int l, int r)
{
int freq[26];
memset (freq, 0, sizeof freq);
for ( int i = 0; i < n; i++) {
if (i < l || i > r)
freq[s[i] - 'a' ]++;
}
int ways = 1;
for ( int i = l; i <= r; i++) {
if (freq[s[i] - 'a' ]) {
ways = ways * freq[s[i] - 'a' ];
freq[s[i] - 'a' ]--;
}
else {
ways = 0;
break ;
}
}
return ways;
}
int main()
{
string s = "cabcaab" ;
int n = s.length();
int l = 1, r = 3;
cout << calculateWays(s, n, l, r);
return 0;
}
|
Java
class GfG {
static int calculateWays(String s, int n, int l, int r)
{
int freq[] = new int [ 26 ];
for ( int i = 0 ; i < n; i++) {
if (i < l || i > r)
freq[s.charAt(i)- 'a' ]++;
}
int ways = 1 ;
for ( int i = l; i <= r; i++) {
if (freq[s.charAt(i) - 'a' ] != 0 ) {
ways = ways * freq[s.charAt(i) - 'a' ];
freq[s.charAt(i) - 'a' ]--;
}
else {
ways = 0 ;
break ;
}
}
return ways;
}
public static void main(String[] args)
{
String s = "cabcaab" ;
int n = s.length();
int l = 1 , r = 3 ;
System.out.println(calculateWays(s, n, l, r));
}
}
|
Python3
def calculateWays(s, n, l, r):
freq = [ 0 for i in range ( 26 )]
for i in range (n):
if (i < l or i > r):
freq[ ord (s[i]) - ord ( 'a' )] + = 1
ways = 1
for i in range (l, r + 1 , 1 ):
if (freq[ ord (s[i]) - ord ( 'a' )]):
ways = ways * freq[ ord (s[i]) - ord ( 'a' )]
freq[ ord (s[i]) - ord ( 'a' )] - = 1
else :
ways = 0
break
return ways
if __name__ = = '__main__' :
s = "cabcaab"
n = len (s)
l = 1
r = 3
print (calculateWays(s, n, l, r))
|
C#
using System;
class GfG
{
static int calculateWays(String s, int n, int l, int r)
{
int []freq = new int [26];
for ( int i = 0; i < n; i++)
{
if (i < l || i > r)
freq[s[i]- 'a' ]++;
}
int ways = 1;
for ( int i = l; i <= r; i++)
{
if (freq[s[i] - 'a' ] != 0) {
ways = ways * freq[s[i] - 'a' ];
freq[s[i] - 'a' ]--;
}
else {
ways = 0;
break ;
}
}
return ways;
}
public static void Main()
{
String s = "cabcaab" ;
int n = s.Length;
int l = 1, r = 3;
Console.WriteLine(calculateWays(s, n, l, r));
}
}
|
PHP
<?php
function calculateWays( $s , $n , $l , $r )
{
$freq = array ();
for ( $i = 0; $i < 26 ; $i ++ )
{
$freq [ $i ] = 0;
}
for ( $i = 0; $i < $n ; $i ++ )
{
if ( $i < $l || $i > $r )
$freq [ord( $s [ $i ]) - 97]++;
}
$ways = 1;
for ( $i = $l ; $i <= $r ; $i ++)
{
if ( $freq [ord( $s [ $i ]) - 97])
{
$ways = $ways * $freq [ord( $s [ $i ]) - 97];
$freq [ord( $s [ $i ]) - 97]--;
}
else
{
$ways = 0;
break ;
}
}
return $ways ;
}
$s = "cabcaab" ;
$n = strlen ( $s );
$l = 1;
$r = 3;
echo calculateWays( $s , $n , $l , $r );
?>
|
Javascript
<script>
function calculateWays( s , n , l , r) {
var freq = Array(26).fill(0);
for (i = 0; i < n; i++) {
if (i < l || i > r)
freq[s.charCodeAt(i) - 'a' .charCodeAt(0)]++;
}
var ways = 1;
for (i = l; i <= r; i++) {
if (freq[s.charCodeAt(i) - 'a' .charCodeAt(0)] != 0) {
ways = ways * freq[s.charCodeAt(i) - 'a' .charCodeAt(0)];
freq[s.charCodeAt(i) - 'a' .charCodeAt(0)]--;
}
else {
ways = 0;
break ;
}
}
return ways;
}
var s = "cabcaab" ;
var n = s.length;
var l = 1, r = 3;
document.write(calculateWays(s, n, l, r));
</script>
|
Time Complexity: O(N), where N is the length of the string. As, we are using a loop to traverse N times to get the frequencies of the characters present in the string.
Auxiliary Space: O(26), as we are using extra space for the freq map.
Last Updated :
18 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...