Count distinct Strings made by removing three consecutive characters
Last Updated :
28 Jul, 2023
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++
#include <iostream>
#include <set>
using namespace std;
void removeTrio(string s)
{
set<string> st;
int n = s.size();
for ( int i = 0; i < n - 2; i++) {
string left = s.substr(0, i);
string right = s.substr(i + 3, n - i - 3);
string new_string = left + right;
st.insert(new_string);
}
cout << st.size() << endl;
return ;
}
int main()
{
string s = "aaaccc" ;
removeTrio(s);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void removeTrio(String s)
{
HashSet<String> st = new HashSet<>();
int n = s.length();
for ( int i = 0 ; i < n - 2 ; i++) {
String left = s.substring( 0 , i);
String right = s.substring(i + 3 , n);
String new_string = left + right;
st.add(new_string);
}
System.out.println(st.size());
return ;
}
public static void main(String[] args)
{
String s = "aaaccc" ;
removeTrio(s);
}
}
|
Python3
from typing import Set
def removeTrio(s: str ) - > None :
st: Set [ str ] = set ()
n: int = len (s)
for i in range (n - 2 ):
left: str = s[:i]
right: str = s[i + 3 :]
new_string: str = left + right
st.add(new_string)
print ( len (st))
return
if __name__ = = "__main__" :
s: str = "aaaccc"
removeTrio(s)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void RemoveTrio( string s)
{
HashSet< string > st = new HashSet< string >();
int n = s.Length;
for ( int i = 0; i < n - 2; i++)
{
string left = s.Substring(0, i);
string right = s.Substring(i + 3, n - i - 3);
string new_string = left + right;
st.Add(new_string);
}
Console.WriteLine(st.Count);
return ;
}
static void Main( string [] args)
{
string s = "aaaccc" ;
RemoveTrio(s);
return ;
}
}
|
Javascript
function removeTrio(s) {
let st = new Set();
let n = s.length;
for (let i = 0; i < n - 2; i++) {
let left = s.slice(0, i);
let right = s.slice(i + 3);
let new_string = left + right;
st.add(new_string);
}
console.log(st.size);
return ;
}
let s = "aaaccc" ;
removeTrio(s);
|
Time Complexity : O(N)
Auxiliary Space: O(N*M)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...