Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Count distinct Strings made by removing three consecutive characters

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given a string S of length N(N > 3), the task is to count distinct strings that can be made by removing three consecutive characters from the original string.

Examples: 

Input: S = “aaabcc”
Output: 4 
Explanation: “bcc”, “acc”, “aac”, “aaa”

Input: S = “aaaaa”
Output: 1 
Explanation: “aaa”

Approach: This can be solved with the following idea:

We have to iterate the string. Then we can concatenate the substrings except for three characters from each iterator and store them in a set for removing duplicity. By this, we can get the number of different strings.

Below are the steps involved in the implementation of the code:

  • Iterate through the string.
  • Maintain a variable for the size of the substring to be removed.
  • Concat the leftover string and insert it into the set.
  • Return the size of the set at the end.

Below is the implementation of the code :

C++




// C++ Implementation of code
#include <iostream>
#include <set>
using namespace std;
 
// Function to count distinct strings
// formed
void removeTrio(string s)
{
 
    // Initialized the set
    set<string> st;
 
    int n = s.size();
 
    for (int i = 0; i < n - 2; i++) {
 
        // Left substring except the trio
        // from i
        string left = s.substr(0, i);
 
        // Right substring except the
        // trio from i
        string right = s.substr(i + 3, n - i - 3);
 
        // New substring after concatining
        // the left and the right substring.
        string new_string = left + right;
 
        // Inserting in the set
        st.insert(new_string);
    }
 
    // Return the size of set
    cout << st.size() << endl;
 
    return;
}
 
// Driver code
int main()
{
 
    string s = "aaaccc";
 
    // Function call
    removeTrio(s);
    return 0;
}


Python3




# Python implementation of code
from typing import Set
 
# Function to count distinct strings formed
def removeTrio(s: str) -> None:
 
    # Initialized the set
    st: Set[str] = set()
 
    n: int = len(s)
 
    for i in range(n - 2):
 
        # Left substring except the trio from i
        left: str = s[:i]
 
        # Right substring except the trio from i
        right: str = s[i+3:]
 
        # New substring after concatining the left and the right substring.
        new_string: str = left + right
 
        # Inserting in the set
        st.add(new_string)
 
    # Return the size of set
    print(len(st))
 
    return
 
# Driver code
if __name__ == "__main__":
 
    s: str = "aaaccc"
 
    # Function call
    removeTrio(s)
 
# This code is contributed by Susobhan Akhuli


Java




// Java code implementation:
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to count distinct strings formed
    static void removeTrio(String s)
    {
 
        // Initialized the set
        HashSet<String> st = new HashSet<>();
 
        int n = s.length();
 
        for (int i = 0; i < n - 2; i++) {
 
            // Left substring except the trio from i
            String left = s.substring(0, i);
 
            // Right substring except the trio from i
            String right = s.substring(i + 3, n);
 
            // New substring after concatenating the left
            // and the right substring.
            String new_string = left + right;
 
            // Inserting in the set
            st.add(new_string);
        }
 
        // Return the size of set
        System.out.println(st.size());
        return;
    }
 
    public static void main(String[] args)
    {
        String s = "aaaccc";
 
        // Function call
        removeTrio(s);
    }
}
 
// This code is contributed by karthik.


Output

4

Time Complexity : O(N)
Auxiliary Space: O(N*M)

Related Articles:


My Personal Notes arrow_drop_up
Last Updated : 14 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials