String | Subsequence & Substring

Last Updated : 26 Sep, 2023

Recent articles on String !

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

Problems on Substring:
Easy Problems

  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

Intermediate Problems

  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

  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

Problems on Subsequences:
Easy Problems

  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

Intermediate Problems

  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
    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:



    Share your thoughts in the comments