Open In App

Count distinct Strings made by removing three consecutive characters

Last Updated : 28 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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;
}


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.


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


C#




using System;
using System.Collections.Generic;
 
class Program
{
      // Function to count distinct strings
    // formed
    static void RemoveTrio(string s)
    {
        // Initialized the set
        HashSet<string> st = new HashSet<string>();
        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 - i - 3);
             
            // New substring after concatining
            // the left and the right substring.
              string new_string = left + right;
            
              // Inserting in the set
              st.Add(new_string);
        }
         
          // Return the size of set
        Console.WriteLine(st.Count);
        return;
    }
 
    static void Main(string[] args)
    {
          // Test case
        string s = "aaaccc";
        RemoveTrio(s);
        return;
    }
}


Javascript




// Function to count distinct strings
// formed
function removeTrio(s) {
    
   // Initialized the set
   let st = new Set();
    let n = s.length;
    for (let i = 0; i < n - 2; i++) {
         
        // Left substring except the trio
        // from i
        let left = s.slice(0, i);
         
        // Right substring except the
        // trio from i
        let right = s.slice(i + 3);
         
        // New substring after concatining
        // the left and the right substring.
        let new_string = left + right;
         
        // Inserting in the set
        st.add(new_string);
    }
     
    // Return the size of set
    console.log(st.size);
    return;
}
 
// Test case
let s = "aaaccc";
removeTrio(s);


Output

4


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

Related Articles:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads