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++
#include <bits/stdc++.h>
using namespace std;
int findSubstringInWraproundString(string p)
{
int ans = 0;
int curLen = 0;
int arr[26] = { 0 };
for ( int i = 0; i < ( int )p.length(); i++) {
int curr = p[i] - 'a' ;
if (i > 0
&& (p[i - 1]
!= ((curr + 26 - 1) % 26 + 'a' ))) {
curLen = 0;
}
curLen++;
if (curLen > arr[curr]) {
ans += (curLen - arr[curr]);
arr[curr] = curLen;
}
}
cout << ans;
}
int main()
{
string p = "zab" ;
findSubstringInWraproundString(p);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findSubstringInWraproundString(String p)
{
int ans = 0 ;
int curLen = 0 ;
int arr[] = new int [ 26 ];
for ( int i = 0 ; i < p.length(); i++)
{
int curr = p.charAt(i) - 'a' ;
if (i > 0
&& (p.charAt(i - 1 )
!= ((curr + 26 - 1 ) % 26 + 'a' )))
{
curLen = 0 ;
}
curLen++;
if (curLen > arr[curr])
{
ans += (curLen - arr[curr]);
arr[curr] = curLen;
}
}
System.out.println(ans);
}
public static void main(String args[])
{
String p = "zab" ;
findSubstringInWraproundString(p);
}
}
|
Python3
def findSubstringInWraproundString(p) :
ans = 0
curLen = 0
arr = [ 0 ] * 26
for i in range ( 0 , len (p)) :
curr = ord (p[i]) - ord ( 'a' )
if (i > 0 and ( ord (p[i - 1 ]) ! = ((curr + 26 - 1 ) % 26 + ord ( 'a' )))) :
curLen = 0
curLen + = 1
if (curLen > arr[curr]) :
ans + = (curLen - arr[curr])
arr[curr] = curLen
print (ans)
p = "zab"
findSubstringInWraproundString(p)
|
C#
using System;
class GFG
{
static void findSubstringInWraproundString( string p)
{
int ans = 0;
int curLen = 0;
int [] arr = new int [26];
for ( int i = 0; i < ( int )p.Length; i++)
{
int curr = p[i] - 'a' ;
if (i > 0 && (p[i - 1] != ((curr + 26 - 1) % 26 + 'a' )))
{
curLen = 0;
}
curLen++;
if (curLen > arr[curr])
{
ans += (curLen - arr[curr]);
arr[curr] = curLen;
}
}
Console.Write(ans);
}
static void Main()
{
string p = "zab" ;
findSubstringInWraproundString(p);
}
}
|
Javascript
<script>
function findSubstringInWraproundString(p)
{
let ans = 0;
let curLen = 0;
let arr = new Array(26);
arr.fill(0);
for (let i = 0; i < p.length; i++)
{
let curr = p[i].charCodeAt() - 'a' .charCodeAt();
if (i > 0 && (p[i - 1].charCodeAt() != ((curr + 26 - 1) % 26 + 'a' .charCodeAt())))
{
curLen = 0;
}
curLen++;
if (curLen > arr[curr])
{
ans += (curLen - arr[curr]);
arr[curr] = curLen;
}
}
document.write(ans);
}
let p = "zab" ;
findSubstringInWraproundString(p);
</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) {
int count[26];
memset (count, 0, sizeof (count));
int len_inc_substring[p.length()];
memset (len_inc_substring, 0, sizeof (len_inc_substring));
for ( int i = 0; i < p.length(); i++) {
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;
}
count[p[i]- 'a' ] = max(count[p[i]- 'a' ], len_inc_substring[i]);
}
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;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static int countSubstringsInWraparoundString(String p) {
int [] count = new int [ 26 ];
Arrays.fill(count, 0 );
int [] len_inc_substring = new int [p.length()];
Arrays.fill(len_inc_substring, 0 );
for ( int i = 0 ; i < p.length(); i++) {
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 ;
}
count[p.charAt(i)- 'a' ] = Math.max(count[p.charAt(i)- 'a' ], len_inc_substring[i]);
}
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));
}
}
|
Python3
def countSubstringsInWraparoundString(p):
count = { chr (i): 0 for i in range ( ord ( 'a' ), ord ( 'z' ) + 1 )}
len_inc_substring = [ 0 ] * len (p)
for i in range ( len (p)):
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
count[p[i]] = max (count[p[i]], len_inc_substring[i])
return sum (count.values())
p = "zab"
print (countSubstringsInWraparoundString(p))
|
C#
using System;
public class MainClass {
public static int
CountSubstringsInWraparoundString( string p)
{
int [] count = new int [26];
Array.Fill(count, 0);
int [] len_inc_substring = new int [p.Length];
Array.Fill(len_inc_substring, 0);
for ( int i = 0; i < p.Length; i++) {
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;
}
count[p[i] - 'a' ] = Math.Max(
count[p[i] - 'a' ], len_inc_substring[i]);
}
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));
}
}
|
Javascript
function countSubstringsInWraparoundString(p) {
let count = {};
for (let i = 'a' .charCodeAt(0); i <= 'z' .charCodeAt(0); i++) {
count[String.fromCharCode(i)] = 0;
}
let len_inc_substring = new Array(p.length).fill(0);
for (let i = 0; i < p.length; i++) {
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;
}
count[p[i]] = Math.max(count[p[i]], len_inc_substring[i]);
}
return Object.values(count).reduce((a,b) => a+b);
}
let p = "zab" ;
console.log(countSubstringsInWraparoundString(p));
|
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
DSA Tutorial - Learn Data Structures and Algorithms
DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
13 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. How do
14 min read
Breadth First Search or BFS for a Graph
Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high. We sort the array using multiple passes. After the fi
8 min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Conditions to apply Binary Search Algorithm in a Data S
15+ min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Selection Sort
Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted. First we find the smallest element a
8 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all
Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1. Note: The given graph does not contain any negative edge. Exampl
12 min read