Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Bitwise Algorithms
  • MCQs on Bitwise Algorithms
  • Tutorial on Biwise Algorithms
  • Binary Representation
  • Bitwise Operators
  • Bit Swapping
  • Bit Manipulation
  • Count Set bits
  • Setting a Bit
  • Clear a Bit
  • Toggling a Bit
  • Left & Right Shift
  • Gray Code
  • Checking Power of 2
  • Important Tactics
  • Bit Manipulation for CP
  • Fast Exponentiation
Open In App
Next Article:
Print all subsequences of a string using ArrayList
Next article icon

Print all subsequences of a string | Iterative Method

Last Updated : 20 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string s, print all possible subsequences of the given string in an iterative manner. We have already discussed Recursive method to print all subsequences of a string. 

Examples:  

Input : abc Output : a, b, c, ab, ac, bc, abc Input : aab Output : a, b, aa, ab, aab

Approach 1 : 
Here, we discuss much easier and simpler iterative approach which is similar to Power Set. We use bit pattern from binary representation of 1 to 2^length(s) – 1.

input = “abc” 
Binary representation to consider 1 to (2^3-1), i.e 1 to 7. 
Start from left (MSB) to right (LSB) of binary representation and append characters from input string which corresponds to bit value 1 in binary representation to Final subsequence string sub.

Example: 

001 => abc . Only c corresponds to bit 1. So, subsequence = c.  101 => abc . a and c corresponds to bit 1. So, subsequence = ac. binary_representation (1) = 001 => c  binary_representation (2) = 010 => b  binary_representation (3) = 011 => bc  binary_representation (4) = 100 => a  binary_representation (5) = 101 => ac  binary_representation (6) = 110 => ab  binary_representation (7) = 111 => abc

Below is the implementation of above approach: 

C++

// C++ program to print all Subsequences
// of a string in iterative manner
#include <bits/stdc++.h>
using namespace std;
 
// function to find subsequence
string subsequence(string s, int binary, int len)
{
    string sub = "";
    for (int j = 0; j < len; j++)
 
        // check if jth bit in binary is 1
        if (binary & (1 << j))
 
            // if jth bit is 1, include it
            // in subsequence
            sub += s[j];
 
    return sub;
}
 
// function to print all subsequences
void possibleSubsequences(string s){
 
    // map to store subsequence
    // lexicographically by length
    map<int, set<string> > sorted_subsequence;
 
    int len = s.size();
     
    // Total number of non-empty subsequence
    // in string is 2^len-1
    int limit = pow(2, len);
     
    // i=0, corresponds to empty subsequence
    for (int i = 1; i <= limit - 1; i++) {
         
        // subsequence for binary pattern i
        string sub = subsequence(s, i, len);
         
        // storing sub in map
        sorted_subsequence[sub.length()].insert(sub);
    }
 
    for (auto it : sorted_subsequence) {
         
        // it.first is length of Subsequence
        // it.second is set<string>
        cout << "Subsequences of length = "
             << it.first << " are:" << endl;
              
        for (auto ii : it.second)
             
            // ii is iterator of type set<string>
            cout << ii << " ";
         
        cout << endl;
    }
}
 
// driver function
int main()
{
    string s = "aabc";
    possibleSubsequences(s);
    return 0;
}
                      
                       

Java

// Java program to print all Subsequences
// of a String in iterative manner
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
 
class Graph{
 
// Function to find subsequence
static String subsequence(String s,
                          int binary,
                          int len)
{
    String sub = "";
     
    for(int j = 0; j < len; j++)
     
        // Check if jth bit in binary is 1
        if ((binary & (1 << j)) != 0)
 
            // If jth bit is 1, include it
            // in subsequence
            sub += s.charAt(j);
 
    return sub;
}
 
// Function to print all subsequences
static void possibleSubsequences(String s)
{
     
    // Map to store subsequence
    // lexicographically by length
    SortedMap<Integer,
              HashSet<String>> sorted_subsequence = new TreeMap<Integer,
                                                                HashSet<String>>();
 
    int len = s.length();
 
    // Total number of non-empty subsequence
    // in String is 2^len-1
    int limit = (int) Math.pow(2, len);
 
    // i=0, corresponds to empty subsequence
    for(int i = 1; i <= limit - 1; i++)
    {
         
        // Subsequence for binary pattern i
        String sub = subsequence(s, i, len);
         
        // Storing sub in map
        if (!sorted_subsequence.containsKey(sub.length()))
            sorted_subsequence.put(
                sub.length(), new HashSet<>());
            sorted_subsequence.get(
                sub.length()).add(sub);
    }
 
    for(Map.Entry<Integer,
                  HashSet<String>> it : sorted_subsequence.entrySet())
    {
         
        // it.first is length of Subsequence
        // it.second is set<String>
        System.out.println("Subsequences of length = " +
                           it.getKey() + " are:");
                            
        for(String ii : it.getValue())
         
            // ii is iterator of type set<String>
            System.out.print(ii + " ");
 
        System.out.println();
    }
}
 
// Driver code
public static void main(String[] args)
{
    String s = "aabc";
     
    possibleSubsequences(s);
}
}
 
// This code is contributed by sanjeev2552
                      
                       

Python3

# Python3 program to print all Subsequences
# of a string in iterative manner
 
# function to find subsequence
def subsequence(s, binary, length):
    sub = ""
    for j in range(length):
         
        # check if jth bit in binary is 1
        if (binary & (1 << j)):
 
            # if jth bit is 1, include it
            # in subsequence
            sub += s[j]
    return sub
 
# function to print all subsequences
def possibleSubsequences(s):
 
    # map to store subsequence
    # lexicographically by length
    sorted_subsequence = {}
 
    length = len(s)
     
    # Total number of non-empty subsequence
    # in string is 2^len-1
    limit = 2 ** length
     
    # i=0, corresponds to empty subsequence
    for i in range(1, limit):
         
        # subsequence for binary pattern i
        sub = subsequence(s, i, length)
         
        # storing sub in map
        if len(sub) in sorted_subsequence.keys():
            sorted_subsequence[len(sub)] = \
             tuple(list(sorted_subsequence[len(sub)]) + [sub])
        else:
            sorted_subsequence[len(sub)] = [sub]
 
    for it in sorted_subsequence:
         
        # it.first is length of Subsequence
        # it.second is set<string>
        print("Subsequences of length =", it, "are:")
        for ii in sorted(set(sorted_subsequence[it])):
             
            # ii is iterator of type set<string>
            print(ii, end = ' ')
        print()
 
# Driver Code
s = "aabc"
possibleSubsequences(s)
 
# This code is contributed by ankush_953
                      
                       

C#

// C# program to print all Subsequences
// of a String in iterative manner
using System;
using System.Collections.Generic;
 
class Graph {
 
  // Function to find subsequence
  static string subsequence(string s, int binary, int len)
  {
    string sub = "";
 
    for (int j = 0; j < len; j++)
 
      // Check if jth bit in binary is 1
      if ((binary & (1 << j)) != 0)
 
        // If jth bit is 1, include it
        // in subsequence
        sub += s[j];
 
    return sub;
  }
 
  // Function to print all subsequences
  static void possibleSubsequences(string s)
  {
 
    // Map to store subsequence
    // lexicographically by length
    SortedDictionary<int, HashSet<string> >
      sorted_subsequence
      = new SortedDictionary<int, HashSet<string> >();
 
    int len = s.Length;
 
    // Total number of non-empty subsequence
    // in String is 2^len-1
    int limit = (int)Math.Pow(2, len);
 
    // i=0, corresponds to empty subsequence
    for (int i = 1; i <= limit - 1; i++) {
 
      // Subsequence for binary pattern i
      string sub = subsequence(s, i, len);
 
      // Storing sub in map
      if (!sorted_subsequence.ContainsKey(sub.Length))
        sorted_subsequence[sub.Length]
        = new HashSet<string>();
      sorted_subsequence[sub.Length].Add(sub);
    }
 
    foreach(var it in sorted_subsequence)
    {
 
      // it.first is length of Subsequence
      // it.second is set<String>
      Console.WriteLine("Subsequences of length = "
                        + it.Key + " are:");
 
      foreach(String ii in it.Value)
 
        // ii is iterator of type set<String>
        Console.Write(ii + " ");
 
      Console.WriteLine();
    }
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    string s = "aabc";
 
    possibleSubsequences(s);
  }
}
 
// This code is contributed by phasing17
                      
                       

Javascript

<script>
 
// Javascript program to print all Subsequences
// of a string in iterative manner
 
// function to find subsequence
function subsequence(s, binary, len)
{
    let sub = "";
    for(let j = 0; j < len; j++)
     
        // Check if jth bit in binary is 1
        if (binary & (1 << j))
 
            // If jth bit is 1, include it
            // in subsequence
            sub += s[j];
 
    return sub;
}
 
// Function to print all subsequences
function possibleSubsequences(s)
{
     
    // map to store subsequence
    // lexicographically by length
    let sorted_subsequence = new Map();
 
    let len = s.length;
 
    // Total number of non-empty subsequence
    // in string is 2^len-1
    let limit = Math.pow(2, len);
 
    // i=0, corresponds to empty subsequence
    for(let i = 1; i <= limit - 1; i++)
    {
         
        // Subsequence for binary pattern i
        let sub = subsequence(s, i, len);
 
        // Storing sub in map
        if (!sorted_subsequence.has(sub.length))
            sorted_subsequence.set(sub.length, new Set());
             
        sorted_subsequence.get(sub.length).add(sub);
    }
 
    for(let it of sorted_subsequence)
    {
         
        // it.first is length of Subsequence
        // it.second is set<string>
        document.write("Subsequences of length = " +
                       it[0] + " are:" + "<br>");
 
        for(let ii of it[1])
 
            // ii is iterator of type set<string>
            document.write(ii + " ");
 
        document.write("<br>");
    }
}
 
// Driver code
let s = "aabc";
 
possibleSubsequences(s);
 
// This code is contributed by gfgking
 
</script>
                      
                       

Output
Subsequences of length = 1 are: a b c  Subsequences of length = 2 are: aa ab ac bc  Subsequences of length = 3 are: aab aac abc  Subsequences of length = 4 are: aabc 

Time Complexity : O(2^n), where n is length of string to find subsequences and n is length of binary string.
Space Complexity: O(1)

Approach 2 : 

Approach is to get the position of rightmost set bit and reset that bit after appending corresponding character from given string to the subsequence and will repeat the same thing till corresponding binary pattern has no set bits.

If input is s = “abc”  Binary representation to consider 1 to (2^3-1), i.e 1 to 7.  001 => abc . Only c corresponds to bit 1. So, subsequence = c  101 => abc . a and c corresponds to bit 1. So, subsequence = ac. Let us use Binary representation of 5, i.e 101.  Rightmost bit is at position 1, append character at beginning of sub = c ,reset position 1 => 100  Rightmost bit is at position 3, append character at beginning of sub = ac ,reset position 3 => 000  As now we have no set bit left, we stop computing subsequence.

Example:

binary_representation (1) = 001 => c  binary_representation (2) = 010 => b  binary_representation (3) = 011 => bc  binary_representation (4) = 100 => a  binary_representation (5) = 101 => ac  binary_representation (6) = 110 => ab  binary_representation (7) = 111 => abc

Below is the implementation of above approach : 

C++

// C++ code all Subsequences of a
// string in iterative manner
#include <bits/stdc++.h>
using namespace std;
 
// function to find subsequence
string subsequence(string s, int binary)
{
    string sub = "";
    int pos;
     
    // loop while binary is greater than 0
    while(binary>0)
    {
        // get the position of rightmost set bit
        pos=log2(binary&-binary)+1;
         
        // append at beginning as we are
        // going from LSB to MSB
        sub=s[pos-1]+sub;
         
        // resets bit at pos in binary
        binary= (binary & ~(1 << (pos-1)));
    }
    reverse(sub.begin(),sub.end());
    return sub;
}
 
// function to print all subsequences
void possibleSubsequences(string s){
 
    // map to store subsequence
    // lexicographically by length
    map<int, set<string> > sorted_subsequence;
 
    int len = s.size();
     
    // Total number of non-empty subsequence
    // in string is 2^len-1
    int limit = pow(2, len);
     
    // i=0, corresponds to empty subsequence
    for (int i = 1; i <= limit - 1; i++) {
         
        // subsequence for binary pattern i
        string sub = subsequence(s, i);
         
        // storing sub in map
        sorted_subsequence[sub.length()].insert(sub);
    }
 
    for (auto it : sorted_subsequence) {
         
        // it.first is length of Subsequence
        // it.second is set<string>
        cout << "Subsequences of length = "
            << it.first << " are:" << endl;
             
        for (auto ii : it.second)
             
            // ii is iterator of type set<string>
            cout << ii << " ";
         
        cout << endl;
    }
}
 
// driver function
int main()
{
    string s = "aabc";
    possibleSubsequences(s);
     
    return 0;
}
                      
                       

Java

import java.util.*;
 
public class GFG {
    // function to find subsequence
    public static String subsequence(String s, int binary) {
        String sub = "";
        int pos;
 
        // loop while binary is greater than 0
        while (binary > 0) {
            // get the position of rightmost set bit
            pos = (int) (Math.log(binary & -binary) / Math.log(2)) + 1;
 
            // append at beginning as we are
            // going from LSB to MSB
            sub = s.charAt(pos - 1) + sub;
 
            // resets bit at pos in binary
            binary = (binary & ~(1 << (pos - 1)));
        }
        sub = new StringBuilder(sub).reverse().toString();
        return sub;
    }
 
    // function to print all subsequences
    public static void possibleSubsequences(String s) {
        // map to store subsequence
        // lexicographically by length
        Map<Integer, Set<String>> sortedSubsequence = new HashMap<>();
 
        int len = s.length();
 
        // Total number of non-empty subsequence
        // in string is 2^len-1
        int limit = (int) Math.pow(2, len);
 
        // i=0, corresponds to empty subsequence
        for (int i = 1; i <= limit - 1; i++) {
            // subsequence for binary pattern i
            String sub = subsequence(s, i);
 
            // storing sub in map
            sortedSubsequence.computeIfAbsent(sub.length(), k -> new TreeSet<>()).add(sub);
        }
 
        for (Map.Entry<Integer, Set<String>> it : sortedSubsequence.entrySet()) {
            // it.getKey() is length of Subsequence
            // it.getValue() is Set<String>
            System.out.println("Subsequences of length = " + it.getKey() + " are:");
 
            for (String ii : it.getValue())
                // ii is element of Set<String>
                System.out.print(ii + " ");
 
            System.out.println();
        }
    }
 
    // driver function
    public static void main(String[] args) {
        String s = "aabc";
        possibleSubsequences(s);
    }
}
                      
                       

Python3

# Python3 program to print all Subsequences
# of a string in an iterative manner
from math import log2, floor
 
# function to find subsequence
def subsequence(s, binary):
    sub = ""
     
    # loop while binary is greater than
    while(binary > 0):
         
        # get the position of rightmost set bit
        pos=floor(log2(binary&-binary) + 1)
         
        # append at beginning as we are
        # going from LSB to MSB
        sub = s[pos - 1] + sub
         
        # resets bit at pos in binary
        binary= (binary & ~(1 << (pos - 1)))
 
    sub = sub[::-1]
    return sub
 
# function to print all subsequences
def possibleSubsequences(s):
 
    # map to store subsequence
    # lexicographically by length
    sorted_subsequence = {}
 
    length = len(s)
     
    # Total number of non-empty subsequence
    # in string is 2^len-1
    limit = 2 ** length
     
    # i=0, corresponds to empty subsequence
    for i in range(1, limit):
         
        # subsequence for binary pattern i
        sub = subsequence(s, i)
         
        # storing sub in map
        if len(sub) in sorted_subsequence.keys():
            sorted_subsequence[len(sub)] = \
            tuple(list(sorted_subsequence[len(sub)]) + [sub])
        else:
            sorted_subsequence[len(sub)] = [sub]
 
    for it in sorted_subsequence:
         
        # it.first is length of Subsequence
        # it.second is set<string>
        print("Subsequences of length =", it, "are:")
        for ii in sorted(set(sorted_subsequence[it])):
             
            # ii is iterator of type set<string>
            print(ii, end = ' ')
        print()
 
# Driver Code
s = "aabc"
possibleSubsequences(s)
 
# This code is contributed by ankush_953
                      
                       

C#

// C# program to print all Subsequences
// of a string in an iterative manner
 
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG
{
 
  // function to find subsequence
  static string subsequence(string s, int binary)
  {
    string sub = "";
 
    // loop while binary is greater than
    while (binary > 0) {
 
      // get the position of rightmost set bit
      var pos = (int)Math.Floor(
        Math.Log(binary & (-binary))
        / Math.Log(2))
        + 1;
 
      // append at beginning as we are
      // going from LSB to MSB
      sub = s[pos - 1] + sub;
 
      // resets bit at pos in binary
      binary = (binary & ~(1 << (pos - 1)));
    }
 
    char[] charArray = sub.ToCharArray();
    Array.Reverse(charArray);
    return new string(charArray);
  }
 
  // function to print all subsequences
  static void possibleSubsequences(string s)
  {
 
    // map to store subsequence
    // lexicographically by length
    Dictionary<int, HashSet<string> > sorted_subsequence
      = new Dictionary<int, HashSet<string> >();
 
    int length = s.Length;
 
    // Total number of non-empty subsequence
    // in string is 2^len-1
    int limit = (int)Math.Pow(2, length);
 
    // i=0, corresponds to empty subsequence
    for (int i = 1; i < limit; i++) {
      // subsequence for binary pattern i
      string sub = subsequence(s, i);
 
      // storing sub in map
      if (sorted_subsequence.ContainsKey(
        sub.Length)) {
        sorted_subsequence[sub.Length].Add(sub);
      }
      else {
        sorted_subsequence[sub.Length]
          = new HashSet<string>();
        sorted_subsequence[sub.Length].Add(sub);
      }
    }
    foreach(var it in sorted_subsequence)
    {
 
      // it.first is length of Subsequence
      // it.second is set<string>
      Console.WriteLine("Subsequences of length = "
                        + it.Key + " are:");
      var arr = (it.Value).ToArray();
      Array.Sort(arr);
      for (int i = 0; i < arr.Length; i++) {
        Console.Write(arr[i] + " ");
      }
      Console.WriteLine();
    }
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    string s = "aabc";
    possibleSubsequences(s);
  }
}
 
// This code is contributed by phasing17
                      
                       

Javascript

// JavaScript program to print all Subsequences
// of a string in an iterative manner
 
// function to find subsequence
function subsequence(s, binary)
{
    var sub = "";
     
    // loop while binary is greater than
    while(binary > 0)
    {
         
        // get the position of rightmost set bit
        var pos= Math.floor(Math.log2(binary&-binary) + 1);
         
        // append at beginning as we are
        // going from LSB to MSB
        sub = s[pos - 1] + sub;
         
        // resets bit at pos in binary
        binary= (binary & ~(1 << (pos - 1)));
    }
 
    sub =  sub.split("");
    sub = sub.reverse();
    return sub.join("");
}
 
// function to print all subsequences
function possibleSubsequences(s)
{
 
    // map to store subsequence
    // lexicographically by length
    var sorted_subsequence = {};
 
    var length = s.length;
     
    // Total number of non-empty subsequence
    // in string is 2^len-1
    var limit = 2 ** length;
     
    // i=0, corresponds to empty subsequence
    for (var i = 1; i < limit; i++)
    {
        // subsequence for binary pattern i
        var sub = subsequence(s, i);
         
        // storing sub in map
        if (sorted_subsequence.hasOwnProperty(sub.length))
        {
            //var list = sorted_subsequence[sub.length];
            //list.push(sub);
            sorted_subsequence[sub.length].push(sub);
        }
        else
            sorted_subsequence[sub.length] = [sub];
    }
    for (const it in sorted_subsequence)
    {
         
        // it.first is length of Subsequence
        // it.second is set<string>
        console.log("Subsequences of length =", it, "are:");
        var arr = sorted_subsequence[it];
        arr.sort();
        var set = new Set(arr);
        for (const ii of set)
            // ii is iterator of type set<string>
            process.stdout.write(ii + " ");
        console.log()
    }
}
 
// Driver Code
var s = "aabc";
possibleSubsequences(s);
 
// This code is contributed by phasing17
                      
                       

Output
Subsequences of length = 1 are: a b c  Subsequences of length = 2 are: aa ab ac bc  Subsequences of length = 3 are: aab aac abc  Subsequences of length = 4 are: aabc 

Time Complexity: [Tex]O(2^{n} * b)                   [/Tex], where n is the length of string to find subsequence and b is the number of set bits in binary string.
Auxiliary Space: O(n)



Next Article
Print all subsequences of a string using ArrayList

A

Abhishek rajput
Improve
Article Tags :
  • Bit Magic
  • Competitive Programming
  • DSA
  • Strings
  • Technical Scripter
  • subsequence
Practice Tags :
  • Bit Magic
  • Strings

Similar Reads

  • Print all subsequences of a string
    Given a string, we have to find out all its subsequences of it. A String is said to be a subsequence of another String, if it can be obtained by deleting 0 or more character without changing its order. Examples: Input : abOutput : "", "a", "b", "ab" Input : abcOutput : "", "a", "b", "c", "ab", "ac",
    12 min read
  • Print all subsequences of a string using ArrayList
    Given a string str, the task is to print all the sub-sequences of str. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.Examples: Input: str = "abc" Output: a b ab c ac bc abcInput: str = "geek"
    8 min read
  • Print all subsequences of a string in Python
    Given a string s of size n (1 ≤ n ≤ 20), the task is to print all subsequences of string. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. Examples: Input: s = "abc"Output: ["", "a", "b", "c",
    3 min read
  • Count All Palindromic Subsequence in a given String
    Given a string s of length n, the task is to count number of palindromic subsequence (need not necessarily be distinct) present in the string s. Example: Input: s = "abcd"Output: 4Explanation: Palindromic subsequence are : "a" ,"b", "c" ,"d" Input: s = "aab"Output: 4Explanation: palindromic subseque
    15+ min read
  • Length of longest increasing subsequence in a string
    Given a string S, the task is to find the length of the longest increasing subsequence present in the given string. A sequence of characters placed in increasing order of their ASCII values is called an increasing sequence. Examples: Input: S = "abcfgffs"Output: 6Explanation: Subsequence "abcfgs" is
    7 min read
  • Check if given String can be split only into subsequences ABC
    Given a string S of length N where each character of the string is either 'A', 'B' or 'C'. The task is to find if is it possible to split the string into subsequences "ABC". If it is possible to split then print "Yes". Otherwise, print "No". Examples: Input: S = "ABABCC"Output: YesExplanation:One of
    12 min read
  • Number of subsequences as "ab" in a string repeated K times
    Given a String S, consider a new string formed by repeating the S exactly K times. We need find the number of subsequences as “ab” in the newly formed string. Examples : Input : S = "abcb" K = 2 Output : 6 Here, Given string is repeated 2 times and we get a new string "abcbabcb" Below are 6 occurren
    10 min read
  • Find number of times a string occurs as a subsequence in given string
    Given two strings, find the number of times the second string occurs in the first string, whether continuous or discontinuous. Examples: Input: string a = "GeeksforGeeks"string b = "Gks"Output: 4Explanation: The four strings are - (Check characters marked in bold)GeeksforGeeksGeeksforGeeksGeeksforGe
    15+ min read
  • Subsequences of given string consisting of non-repeating characters
    Given a string str of length N, the task is to print all possible distinct subsequences of the string str which consists of non-repeating characters only. Examples: Input: str = "abac" Output: a ab abc ac b ba bac bc c Explanation: All possible distinct subsequences of the strings are { a, aa, aac,
    7 min read
  • All substrings of a given String
    Given a string s, containing lowercase alphabetical characters. The task is to print all non-empty substrings of the given string. Examples : Input : s = "abc"Output : "a", "ab", "abc", "b", "bc", "c" Input : s = "ab"Output : "a", "ab", "b" Input : s = "a"Output : "a" [Expected Approach] - Using Ite
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences