Professional Documents
Culture Documents
FAL (2022-23) STS3006 TH AP2022232000027 Reference Material I 18-Nov-2022 L-10 String
FAL (2022-23) STS3006 TH AP2022232000027 Reference Material I 18-Nov-2022 L-10 String
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Weights substring
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Weightes substring
Concept:
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” © 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Example:1
// Add weight to current sum
// Java program to find the count of
sum += Q.charAt(pos) - '0';
// all the sub-Strings with weight of
// characters atmost K
// Add current character to subString
import java.util.*;
s += P.charAt(j);
// If sum of characters is <=K
class GFG{
// then insert into the set
if (sum <= K) {
// Function to find the count of
S.add(s);
// all the subStrings with weight
}
// of characters atmost K
static int distinctSubString(String P, String Q,
else {
int K, int N)
break;
{
}
}
// Hashmap to store all subStrings
}
HashSet<String> S = new HashSet<String>();
// Finding the size of the set
// Iterate over all subStrings
return S.size();
for (int i = 0; i < N; ++i) {
}
// Maintain the sum of all characters
// Driver code
// encountered so far
public static void main(String[] args)
int sum = 0;
{
// Maintain the subString till the
String P = "abcde";
// current position
String Q = "12345678912345678912345678";
String s = "";
int K = 5;
for (int j = i; j < N; ++j) {
int N = P.length();
// Get the position of the
System.out.print(distinctSubString(P, Q, K, N));
// character in String Q
}
int pos = P.charAt(j) - 'a';
}
Output: 7
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example:2
// Java implementation of the approach // Driver code
import java.util.HashMap; public static void main(String []args)
import java.util.Map; {
class GfG String str[] = { "geeksforgeeks",
{// Function to return the required string "algorithms",
score
static int strScore(String str[], String "stack" };
s, int n) String s = "algorithms";
{ int n = str.length;
// create a hash map of strings in System.out.println(strScore(str,
str s, n));
HashMap<String, Integer> m = new
HashMap<>(); }
// Store every string in the map }
// along with its position in the array Output:244
for (int i = 0; i < n; i++)
m.put(str[i], i + 1);
// If given string is not present in str[]
if (!m.containsKey(s))
return 0;
int score = 0;
for (int i = 0; i < s.length(); i++)
score += s.charAt(i) - 'a' +
1;
// Multiply sum of alphabets with position
score = score * m.get(s);
return score;
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Move hyphen to beginning
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Move hyphen to beginning
Definition:
Given a string that has set of words and spaces, write a program to move all spaces to front
of string, by traversing the string only once.
Examples:
Input : str = "geeks for geeks"
Output : ste = " geeksforgeeks"
Input : str = "move these spaces to beginning"
Output : str = " movethesespacestobeginning"
There were four space characters in input,
all of them should be shifted in front:
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example:1(Using Swap)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example:2(Without using swap)
int i = str.length-1; }
}
Output:
for (int j = i; j >= 0; j--)
if (str[j] != ' ') Heythere,it'sGeeksforGeeks
str[i--] = str[j];
while (i >= 0)
str[i--] = ' ';
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Manacher's Algorithm
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Manacher's Algorithm
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Steps of the Manacher's Algorithm
© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Manacher's Algorithm-Example
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Manacher's Algorithm-Example
if (L[i] > maxLPSLength) // Track maxLPSLength String text = "babcbabcbaccba";
{
findLongestPalindromicString(text);
maxLPSLength = L[i];
maxLPSCenterPosition = i;
text = "abaaba";
} findLongestPalindromicString(text);
text = "abababa";
// If palindrome centered at currentRightPosition i findLongestPalindromicString(text);
// expand beyond centerRightPosition R,
text = "abcbabcbabcba";
// adjust centerPosition C based on expanded palindrome.
if (i + L[i] > R) findLongestPalindromicString(text);
{ text = "forgeeksskeegfor";
C = i; findLongestPalindromicString(text);
R = i + L[i];
text = "caba";
}
findLongestPalindromicString(text);
// Uncomment it to print LPS Length array
// printf("%d ", L[i]); text = "abacdfgdcaba";
} findLongestPalindromicString(text);
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Manacher's Algorithm-Example
Output:
start = (maxLPSCenterPosition -
maxLPSLength) / 2;
end = start + maxLPSLength - 1;
System.out.printf("LPS of string is %s : ",
text);
for (i = start; i <= end; i++)
System.out.print(text.charAt(i));
System.out.println();
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Thank you…
© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.