Count all sub-strings with weight of characters atmost K
Given a string P consisting of small English letters and a string Q consisting of weight of all characters of English alphabet such that for all ‘i’, 0 ? Q[i] ? 9. The task is to find the total numbers of unique substring with sum of weights atmost K.
Examples:
Input: P = “ababab”, Q = “12345678912345678912345678”, K = 5
Output: 7
Explanation:
The substrings with the sum of weights of individual characters ? 5 are:
“a”, “ab”, “b”, “bc”, “c”, “d”, “e”
Input: P = “acbacbacaa”, Q = “12300045600078900012345000”, K = 2
Output: 3
Explanation:
The substrings with the sum of weights of individual characters ? 2 are:
“a”, “b”, “aa”
Approach: The idea is to use an unordered set to store the unique values. The following steps are followed to compute the answer:
- Iterate over all the substrings using the nested loops and maintain the sum of the weight of all the characters encountered so far.
- If the sum of characters is not greater than K, then insert it in a hashmap.
- Finally, output the size of the hashmap.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int distinctSubstring(string& P, string& Q,
int K, int N)
{
unordered_set<string> S;
for ( int i = 0; i < N; ++i) {
int sum = 0;
string s;
for ( int j = i; j < N; ++j) {
int pos = P[j] - 'a' ;
sum += Q[pos] - '0' ;
s += P[j];
if (sum <= K) {
S.insert(s);
}
else {
break ;
}
}
}
return S.size();
}
int main()
{
string P = "abcde" ;
string Q = "12345678912345678912345678" ;
int K = 5;
int N = P.length();
cout << distinctSubstring(P, Q, K, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int distinctSubString(String P, String Q,
int K, int N)
{
HashSet<String> S = new HashSet<String>();
for ( int i = 0 ; i < N; ++i) {
int sum = 0 ;
String s = "" ;
for ( int j = i; j < N; ++j) {
int pos = P.charAt(j) - 'a' ;
sum += Q.charAt(pos) - '0' ;
s += P.charAt(j);
if (sum <= K) {
S.add(s);
}
else {
break ;
}
}
}
return S.size();
}
public static void main(String[] args)
{
String P = "abcde" ;
String Q = "12345678912345678912345678" ;
int K = 5 ;
int N = P.length();
System.out.print(distinctSubString(P, Q, K, N));
}
}
|
Python3
def distinctSubstring(P, Q, K, N):
S = set ()
for i in range ( 0 ,N):
sum = 0 ;
s = ''
for j in range (i,N):
pos = ord (P[j]) - 97
sum = sum + ord (Q[pos]) - 48
s + = P[j]
if ( sum < = K):
S.add(s)
else :
break
return len (S)
P = "abcde"
Q = "12345678912345678912345678"
K = 5
N = len (P)
print (distinctSubstring(P, Q, K, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int distinctSubString(String P, String Q,
int K, int N)
{
HashSet<String> S = new HashSet<String>();
for ( int i = 0; i < N; ++i) {
int sum = 0;
String s = "" ;
for ( int j = i; j < N; ++j) {
int pos = P[j] - 'a' ;
sum += Q[pos] - '0' ;
s += P[j];
if (sum <= K) {
S.Add(s);
}
else {
break ;
}
}
}
return S.Count;
}
public static void Main(String[] args)
{
String P = "abcde" ;
String Q = "12345678912345678912345678" ;
int K = 5;
int N = P.Length;
Console.Write(distinctSubString(P, Q, K, N));
}
}
|
Javascript
<script>
function distinctSubString(P, Q, K, N)
{
let S = new Set();
for (let i = 0; i < N; ++i) {
let sum = 0;
let s = "" ;
for (let j = i; j < N; ++j) {
let pos = P[j].charCodeAt() - 'a' .charCodeAt();
sum += Q[pos].charCodeAt() - '0' .charCodeAt();
s += P[j];
if (sum <= K) {
S.add(s);
}
else {
break ;
}
}
}
return S.size;
}
let P = "abcde" ;
let Q = "12345678912345678912345678" ;
let K = 5;
let N = P.length;
document.write(distinctSubString(P, Q, K, N));
</script>
|
Time Complexity: O(N2)
Last Updated :
07 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...