Count unique substrings of a string S present in a wraparound string
Last Updated :
23 Jul, 2025
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
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem