0% found this document useful (0 votes)
4 views

Weightes substring

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Weightes substring

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

WEIGHTED SUBSTRING

TEST TIME ON QUICK, SELECTION SORT

URL: https://forms.gle/L9yNisQKr1XjdLHH9

QR CODE:
WEIGHTED SUBSTRING

INTRODUCTION

⮚ Given a string 'P' consisting of small English letters


and a string 'Q' consisting of weight of all characters of
English alphabet.

⮚ Each alphabet will have a weight w where 0 <= w <= 9

⮚ The task is to find the total numbers of unique


substring with sum of weights atmost K
WEIGHTED SUBSTRING

⮚ Weight of a string ‘s’ can be defined as ;

Weight[S[0] + + ... + Weight[S[Len -


] Weight[S[1]] Weight[S[2]] 1]]
Where; Len is length of string
WEIGHTED SUBSTRING

Examples

Sample IO #1 : Sample IO #2 :
P = “abcde” P = “acbacbacaa”
Q = Q =
“12345678912345678912345678 “12300045600078900012345000
k
” = 5 k
” = 2
Output = Output = 3
Explanation :
“a”, “b”, “aa”

The idea is to use HashSet to solve it!


WEIGHTED SUBSTRING

Algorithm

1. Iterate over all the substrings using the nested loops


and maintain the sum of the weight of all the
characters encountered so far.

2. If the sum of characters is not greater than K, then


insert it in a hashmap.

3. Finally, output the size of the hashmap.


WEIGHTED SUBSTRING

import java.util.*; // Add weight to current sum


sum += Q.charAt(pos) - '0';
class EthCode { // Add current character to
static int distinctSubString(String P, subString
String Q, int K, int N) { s += P.charAt(j);
// Hashmap to store all subStrings // If sum of characters is <=K then
HashSet < String > S = new HashSet < insert in into the set
String > (); if (sum <= K) {
// Iterate over all subStrings S.add(s);
for (int i = 0; i < N; ++i) { } else {
// Maintain the sum of all characters break;
// encountered so far }
int sum = 0; }
// Maintain the subString till the }
// current position // required value
String s = ""; return S.size();
for (int j = i; j < N; ++j) { }
// Get the position of the
// character in String Q
int pos = P.charAt(j) - 'a';
WEIGHTED SUBSTRING

public static void main(String[] args)


{
//Sample Input
String P = "abcde";
String Q =
"12345678912345678912345678";
int K = 5;
int N = P.length();
System.out.print(distinctSubString(P,
Q, K, N));
}
}
WEIGHTED SUBSTRING

Time Complexity

QuadraticTime Complexity: O(n^n)


WEIGHTED SUBSTRING

⮚ This is the variation of the previous


program
⮚ Given string ‘S’ which contains only
lowercase characters and an integer ‘K’
⮚ Have to find number of substrings having
weight equal to ‘K’

Sample IO #1 : S = “abcdef”, K=5

Output = 3

Sample IO #2 : S = “abcdef”, K=4

Output = 2
WEIGHTED SUBSTRING

Time Complexity

QuadraticTime Complexity: O(n^n)


WEIGHTED SUBSTRING

import java.util.*; int currWeight = ch - 'a'


class EthCode { int pos = P.charAt(j) - 'a';
static int distinctSubString(String // Add weight to current sum
str, int K, int N) { sum += currweight;
// Hashmap to store all subStrings // Add current character to
HashSet < String > S = new HashSet < subString
String > (); s += P.charAt(j);
// Iterate over all subStrings // If sum of characters is ==K then
for (int i = 0; i < N; ++i) { insert in into the set
// Maintain the sum of all characters if (sum == K) {
// encountered so far S.add(s);
int sum = 0; } else {
// Maintain the subString till the break;
// current position }
String s = ""; }
for (int j = i; j < N; ++j) { }
char ch = str[j]; // required value
//Get the current weight return S.size();
}
WEIGHTED SUBSTRING

public static void main(String[] args)


{
//Sample Input
String str = "abcde";

int K = 5;

//Length of the string


int N = P.length();

//Sample Output
System.out.print(distinctSubString(str,
K, N));
}
}
/ Ethnus /ethnus /
ethnuscodemith Codemithra code_mithra
ra

https://
learn.codemithra.com

[email protected] +91 7815 095 +91 9019 921


om 095 340

You might also like