The Wayback Machine - https://web.archive.org/web/20240609093347/https://www.geeksforgeeks.org/string-subsequence-substring/
Open In App

String, Subsequence & Substring

Last Updated : 22 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

What is a Substring?

A substring is a contiguous part of a string, i.e., a string inside another string.

In general, for an string of size n, there are n*(n+1)/2 non-empty substrings. For example, Consider the string “geeks”, There are 15 non-empty substrings. The subarrays are:

g, ge, gee, geek, geeks,
e, ee, eek, eeks,
e, ek, eks,
k, ks,
ks

What is a Subsequence?

A subsequence is a sequence that can be derived from another sequence by removing zero or more elements, without changing the order of the remaining elements.

More generally, we can say that for a sequence of size n, we can have ((2^n)-1) non-empty sub-sequences in total. For the same above example, there are 15 sub-sequences. They are:

g, e, e, k, s,
ge, ge, gk, gs, ee, ek, es, ek, es, ks,
gee, gek, ges, gek, ges, gks, eek, ees, eks, eks,
geek, gees, eeks,
geeks

Easy Problems on Substring:

  1. Number of substrings of one string present in other
  2. Print all substring of a number without any conversion
  3. Substring Reverse Pattern
  4. Find the count of palindromic sub-string of a string in its sorted form
  5. Check if a string contains a palindromic sub-string of even length
  6. Count number of substrings with numeric value greater than X
  7. Check if the given string is K-periodic
  8. Maximum count of sub-strings of length K consisting of same characters
  9. Check whether given string can be generated after concatenating given strings
  10. Queries to answer the X-th smallest sub-string lexicographically
  11. Count of sub-strings of length n possible from the given string
  12. Longest sub string of 0’s in a binary string which is repeated K times
  13. Length of longest substring having all characters as K
  14. Maximum length palindromic substring such that it starts and ends with given char
  15. Find distinct characters in distinct substrings of a string
  16. Count all substrings having character K
  17. Reverse the given string in the range [L, R]
  18. Number of substrings that start with “geeks” and end with “for”
  19. Repeat substrings of the given String required number of times
  20. Split the binary string into substrings with equal number of 0s and 1s

Medium Problems on Substring

  1. Reverse substrings between each pair of parenthesis
  2. Longest substring with K unique characters using Binary Search
  3. Jaro and Jaro-Winkler similarity
  4. Longest substring with atmost K characters from the given set of characters
  5. Lexicographically all Shortest Palindromic Substrings from a given string
  6. Shortest Palindromic Substring
  7. Count of all unique substrings with non-repeating characters
  8. Check if the given string is shuffled substring of another string
  9. Count of K-size substrings having palindromic permutations
  10. Count of substrings of length K with exactly K distinct characters
  11. Permutation of given string that maximizes count of Palindromic substrings
  12. Check if a substring can be Palindromic by replacing K characters for Q queries
  13. Count of ways to split given string into two non-empty palindromes
  14. Maximize partitions such that no two substrings have any common character
  15. Minimum replacements in a string to make adjacent characters unequal
  16. Count of substrings containing only the given character
  17. Count distinct substrings of a string using Rabin Karp algorithm
  18. Count of substrings having all distinct characters
  19. Smallest String consisting of a String S exactly K times as a Substring
  20. Minimize splits to generate monotonous Substrings from given String

Hard Problems on Substring

  1. Count of Distinct Substrings occurring consecutively in a given String
  2. Check if a String contains Anagrams of length K which does not contain the character X
  3. Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
  4. Minimum size substring to be removed to make a given string palindromic
  5. Count ways to split a Binary String into three substrings having equal count of zeros
  6. Applications of String Matching Algorithms
  7. Minimum operations to transform given string to another by moving characters to front or end
  8. Count characters to be shifted from the start or end of a string to obtain another string
  9. Lexicographic rank of a string among all its substrings
  10. Count of substrings of a string containing another given string as a substring
  11. Count substrings of same length differing by a single character from two given strings
  12. Extract substrings between any pair of delimiters
  13. Longest substring where all the characters appear at least K times | Set 3
  14. Split a string into maximum number of unique substrings
  15. Find the last player to be able to flip a character in a Binary String
  16. Check if a string can be split into 3 substrings such that one of them is a substring of the other two
  17. Count ways to partition a number into increasing sequences of digits
  18. Maximum length of a substring required to be flipped repeatedly to make all characters of binary string equal to 0
  19. XOR of all substrings of a given Binary String
  20. Sub-strings of a string that are prefix of the same string

Easy Problems on Subsequences:

  1. Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
  2. Minimum number of palindromic subsequences to be removed to empty a binary string
  3. Find largest word in dictionary by deleting some characters of given string
  4. Longest Common Anagram Subsequence
  5. Number of subsequences as “ab” in a string repeated K times
  6. Maximum length subsequence possible of the form R^N K^N
  7. Check if two same sub-sequences exist in a string or not
  8. Longest subsequence where each character occurs at least k times
  9. Longest Uncommon Subsequence
  10. Find the lexicographically largest palindromic Subsequence of a String

Medium Problems on Subsequences:

  1. Maximum number of removals of given subsequence from a string
  2. Check if there exists any sub-sequence in a string which is not palindrome
  3. Number of balanced bracket subsequence of length 2 and 4
  4. Minimum cost to make a string free of a subsequence
  5. Longest subsequence having greater corner values
  6. Longest subsequence with at least one character appearing in every string
  7. Distinct strings such that they contains given strings as sub-sequences
  8. Smallest Palindromic Subsequence of Even Length in Range [L, R]
  9. Minimum number of subsequences required to convert one string to another
  10. Find the length of the longest subsequence with first K alphabets having same frequency

Hard Problems on Subsequences:

  1. Number of ways to partition a string into two balanced subsequences
  2. Count common subsequence in two strings
  3. Total number of odd length palindrome sub-sequence around each center
  4. Minimum number of subsequences required to convert one string to another using Greedy Algorithm
  5. Longest Palindromic Subsequence of two distinct characters
  6. Longest subsequence with different adjacent characters
  7. Frequency of maximum occurring subsequence in given string
  8. Convert given string to another by minimum replacements of subsequences by its smallest character
  9. Minimize deletions in a Binary String to remove all subsequences of the form “0101”
  10. Subsequences of given string consisting of non-repeating characters

Quick Links:



Previous Article
Next Article

Similar Reads

Find the longest subsequence of a string that is a substring of another string
Given two strings X and Y consisting of N and M characters, the task is to find the longest subsequence of a string X which is a substring of the string Y. Examples: Input: X = "ABCD", Y = "ACDBDCD"Output: ACDExplanation:"ACD" is longest subsequence of X which is substring of Y. Input: X = A, Y = AOutput: A Naive Approach: The simplest approach to
10 min read
Length of smallest substring of a given string which contains another string as subsequence | Set 2
Given two strings A and B, the task is to find the smallest substring of A having B as a subsequence. Examples: Input: A = "abcdefababaef", B = "abf"Output: 5Explanation:Smallest substring of A having B as subsequence is abcdef.Therefore, the required length is 5. Input: A = "abcdefababaef", B = "aef"Output: 3 Recommended: Please solve it on "PRACT
12 min read
Find length of smallest substring of a given string which contains another string as subsequence
Given two strings A and B, the task is to find the smallest substring of A having B as a subsequence. If there are multiple such substrings in A, return the substring that has the smallest starting index. Examples : Input : A = "abcedbaced" B = "bed"Output : "bced"Explanation : The substring A[2 : 5] is the shortest substring that contains the stri
15 min read
Find length of longest subsequence of one string which is substring of another string
Given two strings X and Y. The task is to find the length of the longest subsequence of string X which is a substring in sequence Y. Examples: Input : X = "ABCD", Y = "BACDBDCD" Output : 3 "ACD" is longest subsequence of X which is substring of Y. Input : X = "A", Y = "A" Output : 1Recommended PracticeFind length of longest subsequenceTry It! PRERE
21 min read
Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'nd substring. Examples: Input : "11235813" Output : ["1", "1", "2", "3", "5", "8", "13"] Input : "1111223" Output : ["1", "11", "12", "23"] Input : "1111213" Output : ["11", "1", "12", "13"] Input : "11121114" Output : []Iterate through the given string by pickin
9 min read
Check if a string can be split into two substrings such that one substring is a substring of the other
Given a string S of length N, the task is to check if a string can be split into two substrings, say A and B such that B is a substring of A. If not possible, print No. Otherwise, print Yes. Examples : Input: S = "abcdab"Output: YesExplanation: Considering the two splits to be A="abcd" and B="ab", B is a substring of A. Input: S = "abcd"Output: No
5 min read
Count occurrences of substring X before every occurrence of substring Y in a given string
Given three strings S, X, and Y consisting of N, A, and B characters respectively, the task is to find the number of occurrences of the substring X before every occurrence of the substring Y in the given string S. Examples: Input S = ”abcdefdefabc”, X = ”def”, Y = ”abc”Output: 0 2Explanation:First occurrence of Y(= "abc"): No of occurrences of X(=
6 min read
Longest substring whose any non-empty substring not prefix or suffix of given String
Given a string S of length N, the task is to find the length of the longest substring X of the string S such that: No non-empty substring of X is a prefix of S.No non-empty substring of X is a suffix of S.If no such string is possible, print −1. Examples: Input: S = "abcdefb"Output: 4Explanation: cdef is the substring satisfying the conditions. Inp
5 min read
Minimum removals to make a string concatenation of a substring of 0s followed by a substring of 1s
Given binary string str of length N​​​​, the task is to find the minimum number of characters required to be deleted from the given binary string to make a substring of 0s followed by a substring of 1s. Examples: Input: str = "00101101"Output: 2Explanation: Removing str[2] and str[6] or removing str[3] and str[6] modifies the given binary string to
11 min read
Find if a given string can be represented from a substring by iterating the substring “n” times
Given a string 'str', check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. Examples: Input: str = "abcabcabc" Output: true The given string is 3 times repetition of "abc" Input: str = "abadabad" Output: true The given string is 2 times repetition of "abad" Input: str = "aabaabaabaab" Ou
20 min read
Article Tags :
Practice Tags :