Count unique substrings of a string S present in a wraparound string
Last Updated :
14 Apr, 2023
Given a string S which is an infinite wraparound string of the string "abcdefghijklmnopqrstuvwxyz", the task is to count the number of unique non-empty substrings of a string p are present in s.
Examples:
Input: S = "zab"
Output: 6
Explanation: All possible substrings are "z", "a", "b", "za", "ab", "zab".
Input: S = "cac"
Output: 2
Explanation: All possible substrings are "a" and "c" only.
Approach: Follow the steps below to solve the problem
- Iterate over each character of the string
- Initialize an auxiliary array arr[] of size 26, to store the current length of substring that is present in string S starting from each character of string P.
- Initialize a variable, say curLen, which stores the length of substring present in P including the current character if the current character is not a part of the previous substring.
- Initialize a variable, say ans, to store the unique count of non-empty substrings of p present in S.
- Iterate over the characters of the string and check for the following two cases:
- Check if the current character can be added with previous substring to form the required substring or not.
- Add the difference of curLen and arr[curr] to ans if (curLen + 1) is greater than arr[curr] to avoid repetition of substrings.
- Print the value of ans.
Below is the implementation of the above approach:
C++
// C++ program for
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the count of
// non-empty substrings of p present in s
int findSubstringInWraproundString(string p)
{
// Stores the required answer
int ans = 0;
// Stores the length of
// substring present in p
int curLen = 0;
// Stores the current length
// of substring that is
// present in string s starting
// from each character of p
int arr[26] = { 0 };
// Iterate over the characters of the string
for (int i = 0; i < (int)p.length(); i++) {
int curr = p[i] - 'a';
// Check if the current character
// can be added with previous substring
// to form the required substring
if (i > 0
&& (p[i - 1]
!= ((curr + 26 - 1) % 26 + 'a'))) {
curLen = 0;
}
// Increment current length
curLen++;
if (curLen > arr[curr]) {
// To avoid repetition
ans += (curLen - arr[curr]);
// Update arr[cur]
arr[curr] = curLen;
}
}
// Print the answer
cout << ans;
}
// Driver Code
int main()
{
string p = "zab";
// Function call to find the
// count of non-empty substrings
// of p present in s
findSubstringInWraproundString(p);
return 0;
}
Java
import java.util.*;
class GFG
{
// Function to find the count of
// non-empty substrings of p present in s
static void findSubstringInWraproundString(String p)
{
// Stores the required answer
int ans = 0;
// Stores the length of
// substring present in p
int curLen = 0;
// Stores the current length
// of substring that is
// present in string s starting
// from each character of p
int arr[] = new int[26];
// Iterate over the characters of the string
for (int i = 0; i < p.length(); i++)
{
int curr = p.charAt(i) - 'a';
// Check if the current character
// can be added with previous substring
// to form the required substring
if (i > 0
&& (p.charAt(i - 1)
!= ((curr + 26 - 1) % 26 + 'a')))
{
curLen = 0;
}
// Increment current length
curLen++;
if (curLen > arr[curr])
{
// To avoid repetition
ans += (curLen - arr[curr]);
// Update arr[cur]
arr[curr] = curLen;
}
}
// Print the answer
System.out.println(ans);
}
// Driver Code
public static void main(String args[])
{
String p = "zab";
// Function call to find the
// count of non-empty substrings
// of p present in s
findSubstringInWraproundString(p);
}
}
// This code is contributed by hemanth gadarla
Python3
# Python3 program for
# the above approach
# Function to find the count of
# non-empty substrings of p present in s
def findSubstringInWraproundString(p) :
# Stores the required answer
ans = 0
# Stores the length of
# substring present in p
curLen = 0
# Stores the current length
# of substring that is
# present in string s starting
# from each character of p
arr = [0]*26
# Iterate over the characters of the string
for i in range(0, len(p)) :
curr = ord(p[i]) - ord('a')
# Check if the current character
# can be added with previous substring
# to form the required substring
if (i > 0 and (ord(p[i - 1]) != ((curr + 26 - 1) % 26 + ord('a')))) :
curLen = 0
# Increment current length
curLen += 1
if (curLen > arr[curr]) :
# To avoid repetition
ans += (curLen - arr[curr])
# Update arr[cur]
arr[curr] = curLen
# Print the answer
print(ans)
p = "zab"
# Function call to find the
# count of non-empty substrings
# of p present in s
findSubstringInWraproundString(p)
# This code is contributed by divyeshrabadiya07.
C#
// C# program for
// the above approach
using System;
class GFG
{
// Function to find the count of
// non-empty substrings of p present in s
static void findSubstringInWraproundString(string p)
{
// Stores the required answer
int ans = 0;
// Stores the length of
// substring present in p
int curLen = 0;
// Stores the current length
// of substring that is
// present in string s starting
// from each character of p
int[] arr = new int[26];
// Iterate over the characters of the string
for (int i = 0; i < (int)p.Length; i++)
{
int curr = p[i] - 'a';
// Check if the current character
// can be added with previous substring
// to form the required substring
if (i > 0 && (p[i - 1] != ((curr + 26 - 1) % 26 + 'a')))
{
curLen = 0;
}
// Increment current length
curLen++;
if (curLen > arr[curr])
{
// To avoid repetition
ans += (curLen - arr[curr]);
// Update arr[cur]
arr[curr] = curLen;
}
}
// Print the answer
Console.Write(ans);
}
// Driver code
static void Main()
{
string p = "zab";
// Function call to find the
// count of non-empty substrings
// of p present in s
findSubstringInWraproundString(p);
}
}
// This code is contributed by divyesh072019.
JavaScript
<script>
// Javascript program for the above approach
// Function to find the count of
// non-empty substrings of p present in s
function findSubstringInWraproundString(p)
{
// Stores the required answer
let ans = 0;
// Stores the length of
// substring present in p
let curLen = 0;
// Stores the current length
// of substring that is
// present in string s starting
// from each character of p
let arr = new Array(26);
arr.fill(0);
// Iterate over the characters of the string
for (let i = 0; i < p.length; i++)
{
let curr = p[i].charCodeAt() - 'a'.charCodeAt();
// Check if the current character
// can be added with previous substring
// to form the required substring
if (i > 0 && (p[i - 1].charCodeAt() != ((curr + 26 - 1) % 26 + 'a'.charCodeAt())))
{
curLen = 0;
}
// Increment current length
curLen++;
if (curLen > arr[curr])
{
// To avoid repetition
ans += (curLen - arr[curr]);
// Update arr[cur]
arr[curr] = curLen;
}
}
// Print the answer
document.write(ans);
}
let p = "zab";
// Function call to find the
// count of non-empty substrings
// of p present in s
findSubstringInWraproundString(p);
// This code is contributed by surehs07.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Method 2:
Approach Steps:
- Initialize a dictionary to store the count of distinct substrings starting with each letter of the English alphabet.
- Initialize the length of the longest increasing substring ending at each position in the given string 'p' to 0.
- Iterate over the characters of 'p' and update the length of the longest increasing substring ending at each position using dynamic programming.
- Update the count of distinct substrings starting with each letter of 'p' based on the length of the longest increasing substring ending at each position.
- Return the sum of counts for all letters.
C++
#include <iostream>
#include <cstring>
using namespace std;
int countSubstringsInWraparoundString(string p) {
// Initialize an array to store the count
// of distinct substrings starting with each letter
int count[26];
memset(count, 0, sizeof(count));
// Initialize the length of the longest increasing
// substring ending at each position to 0
int len_inc_substring[p.length()];
memset(len_inc_substring, 0, sizeof(len_inc_substring));
// Iterate over the characters of the string
for (int i = 0; i < p.length(); i++) {
// Update the length of the longest increasing
// substring ending at the current position
if (i > 0 && (p[i] - p[i-1] + 26) % 26 == 1) {
len_inc_substring[i] = len_inc_substring[i-1] + 1;
}
else {
len_inc_substring[i] = 1;
}
// Update the count of distinct substrings
// starting with the current letter
count[p[i]-'a'] = max(count[p[i]-'a'], len_inc_substring[i]);
}
// Return the sum of counts for all letters
int total_count = 0;
for (int i = 0; i < 26; i++) {
total_count += count[i];
}
return total_count;
}
int main() {
string p = "zab";
cout << countSubstringsInWraparoundString(p) << endl; // Output: 6
return 0;
}
Java
// Java code to count the number of distinct substrings in a wraparound string
import java.util.Arrays;
public class Main {
public static int countSubstringsInWraparoundString(String p) {
// Initialize an array to store the count
// of distinct substrings starting with each letter
int[] count = new int[26];
Arrays.fill(count, 0);
// Initialize the length of the longest increasing
// substring ending at each position to 0
int[] len_inc_substring = new int[p.length()];
Arrays.fill(len_inc_substring, 0);
// Iterate over the characters of the string
for (int i = 0; i < p.length(); i++) {
// Update the length of the longest increasing
// substring ending at the current position
if (i > 0 && (p.charAt(i) - p.charAt(i-1) + 26) % 26 == 1) {
len_inc_substring[i] = len_inc_substring[i-1] + 1;
}
else {
len_inc_substring[i] = 1;
}
// Update the count of distinct substrings
// starting with the current letter
count[p.charAt(i)-'a'] = Math.max(count[p.charAt(i)-'a'], len_inc_substring[i]);
}
// Return the sum of counts for all letters
int total_count = 0;
for (int i = 0; i < 26; i++) {
total_count += count[i];
}
return total_count;
}
public static void main(String[] args) {
String p = "zab";
System.out.println(countSubstringsInWraparoundString(p)); // Output: 6
}
}
Python3
def countSubstringsInWraparoundString(p):
# Initialize a dictionary to store the count
# of distinct substrings starting with each letter
count = {chr(i): 0 for i in range(ord('a'), ord('z')+1)}
# Initialize the length of the longest increasing
# substring ending at each position to 0
len_inc_substring = [0] * len(p)
# Iterate over the characters of the string
for i in range(len(p)):
# Update the length of the longest increasing
# substring ending at the current position
if i > 0 and (ord(p[i]) - ord(p[i-1])) % 26 == 1:
len_inc_substring[i] = len_inc_substring[i-1] + 1
else:
len_inc_substring[i] = 1
# Update the count of distinct substrings
# starting with the current letter
count[p[i]] = max(count[p[i]], len_inc_substring[i])
# Return the sum of counts for all letters
return sum(count.values())
# Test the function with a sample input
p = "zab"
print(countSubstringsInWraparoundString(p)) # Output: 6
C#
using System;
public class MainClass {
public static int
CountSubstringsInWraparoundString(string p)
{
// Initialize an array to store the count
// of distinct substrings starting with each letter
int[] count = new int[26];
Array.Fill(count, 0);
// Initialize the length of the longest increasing
// substring ending at each position to 0
int[] len_inc_substring = new int[p.Length];
Array.Fill(len_inc_substring, 0);
// Iterate over the characters of the string
for (int i = 0; i < p.Length; i++) {
// Update the length of the longest increasing
// substring ending at the current position
if (i > 0 && (p[i] - p[i - 1] + 26) % 26 == 1) {
len_inc_substring[i]
= len_inc_substring[i - 1] + 1;
}
else {
len_inc_substring[i] = 1;
}
// Update the count of distinct substrings
// starting with the current letter
count[p[i] - 'a'] = Math.Max(
count[p[i] - 'a'], len_inc_substring[i]);
}
// Return the sum of counts for all letters
int total_count = 0;
for (int i = 0; i < 26; i++) {
total_count += count[i];
}
return total_count;
}
public static void Main()
{
string p = "zab";
Console.WriteLine(CountSubstringsInWraparoundString(
p)); // Output: 6
}
}
JavaScript
function countSubstringsInWraparoundString(p) {
// Initialize a dictionary to store the count
// of distinct substrings starting with each letter
let count = {};
for (let i = 'a'.charCodeAt(0); i <= 'z'.charCodeAt(0); i++) {
count[String.fromCharCode(i)] = 0;
}
// Initialize the length of the longest increasing
// substring ending at each position to 0
let len_inc_substring = new Array(p.length).fill(0);
// Iterate over the characters of the string
for (let i = 0; i < p.length; i++) {
// Update the length of the longest increasing
// substring ending at the current position
if (i > 0 && (p.charCodeAt(i) - p.charCodeAt(i-1) + 26) % 26 == 1) {
len_inc_substring[i] = len_inc_substring[i-1] + 1;
} else {
len_inc_substring[i] = 1;
}
// Update the count of distinct substrings starting with the current letter
count[p[i]] = Math.max(count[p[i]], len_inc_substring[i]);
}
// Return the sum of counts for all letters
return Object.values(count).reduce((a,b) => a+b);
}
// Test the function with a sample input
let p = "zab";
console.log(countSubstringsInWraparoundString(p)); // Output: 6
Time Complexity:
The time complexity of this approach is O(n), where n is the length of the input string 'p'. This is because we iterate over the characters of 'p' only once.
Auxiliary Space:
The auxiliary space of this approach is O(26), which is constant. This is because we use a dictionary to store the count of distinct substrings starting with each letter of the English alphabet, and the size of the dictionary is fixed at 26. We also use a list of size n to store the length of the longest increasing substring ending at each position in 'p'. Therefore, the total auxiliary space used by the algorithm is O(26 + n), which is equivalent to O(n).
Similar Reads
Frequency of a substring in a string | Set 2
Given a string str of length N and a substring pattern of length M, the task is to find the frequency of occurrences of pattern as a substring in the given string. If pattern is present in the string str, then print "Yes" with the count of its occurrence. Otherwise, print "No". Examples: Input: str
6 min read
Contiguous unique substrings with the given length L
Given a string str and an integer L. The task is to print all the unique substring of length L from string str. Examples: Input: str = "abca", L=3 Output: "abc", "bca" Input: str = "aaaa", L=3 Output: "aaa" Approach: Firstly generate all the substring of length L and then by using set we can insert
4 min read
Number of sub-strings which are anagram of any sub-string of another string
Given two strings S1 and S2, the task is to count the number of sub-strings of S1 that are anagrams of any sub-string of S2. Examples: Input: S1 = "ABB", S2 = "BAB" Output: 5 There are 6 sub-strings of S1 : "A", "B", "B", "AB", "BB" and "ABB" Out of which only "BB" is the one which is not an anagram
10 min read
C++ Program To Check If A String Is Substring Of Another
Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output
4 min read
Count of sub-strings of length n possible from the given string
Given a string str and an integer N, the task is to find the number of possible sub-strings of length N.Examples: Input: str = "geeksforgeeks", n = 5 Output: 9 All possible sub-strings of length 5 are "geeks", "eeksf", "eksfo", "ksfor", "sforg", "forge", "orgee", "rgeek" and "geeks".Input: str = "jg
6 min read
Count substrings that contain all vowels | SET 2
Given a string str containing lowercase alphabets, the task is to count the sub-strings that contain all the vowels at-least one time and there are no consonants (non-vowel characters) present in the sub-strings.Examples: Input: str = "aeoibsddaaeiouudb" Output: 4 Explanation: The 4 distinct substri
12 min read
Count distinct substrings that contain some characters at most k times
Given a integer k and a string str, the task is to count the number of distinct sub-strings such that each sub-string does not contain some specific characters more than k times. The specific characters are given as another string. Examples: Input: str = "ababab", anotherStr = "bcd", k = 1 Output: 5
7 min read
Count of strings that become equal to one of the two strings after one removal
Given two strings str1 and str2, the task is to count all the valid strings. An example of a valid string is given below: If str1 = "toy" and str2 = "try". Then S = "tory" is a valid string because when a single character is removed from it i.e. S = "tory" = "try" it becomes equal to str1. This prop
9 min read
Find total no of collisions taking place between the balls in which initial direction of each ball is given
Given N balls in a line. The initial direction of each ball is represented by the string consists of only 'L' and 'R' for the left and right direction respectively. It is given that both the balls reverse their direction after the collision and speed will remain the same before and after the collisi
5 min read
Prefixes with more a than b
Given a string S consisting of only characters 'a' and 'b', and an integer N. The string S is added N times to obtain string T. Your task is to count the number of prefixes where number of a is strictly greater than b. string T = S + S + S + S ....... N times. Examples : Input : aba 2 Output : 5 Exp
11 min read