Longest Substring containing C2, starting with C1 and ending with C3


A substring is a string that can be achieved from the given string by removing some characters from the beginning and end of the string (possibly none or all). We are given a string and three characters and have to find the longest substring that contains all the three given characters in the sequence of c1, c2, and c3 starting with c1 and ending with c3. Also, the given characters may be the same but we string must contain different characters for each of them.

Input

string str = "abacdeab"
char c1 = a 
char c2 = b
char c3 = c

Output

"abac"

Explanation

We have only three possible indexes from where a substring starts from the character 'a' which are 0, 2, and 6. Now substrings can end at only character 'c' that is present at index 3.

For the third condition character 'b' must be present in the substring making the substring from 0 to 3 the required string.

Approach

We have seen the examples above, and now let us move to see the steps required to implement the program.

  • First, we have to define a function that will take a string and all three given characters as the parameters and will return an integer value as the return value.

  • In the function, we will get the length of the string and create a integer variable to find the first instance of the character first.

  • We will traverse over the string using that variable and break the loop when the first instance of the character1 is found, if it does not find then we will return an empty string.

  • Now, we will create a variable to mark whether we have found character 2 or not and another variable to store the last position of character 3.

  • We will traverse over the string from the next index where the first instance of the first character was found and look for the second and third characters.

  • If the second character occurs at any position then we mark it as found and if we tackle the third character then we check if we have already found the second character then we will mark the third character's position.

  • At last, we will return the substring from the given string in between the pointer's first and third characters.

Example

#include <bits/stdc++.h>
using namespace std;

// function to find the required substring 
string getString(string str, char char1, char char2, char char3){
   int len = str.length(); // getting length of the string     
   // traversing over the array to get the first occurrence of char1
   int i = 0;
   for(; i<len; i++){
      // if current character is char1 break
      if(str[i] == char1){
         break;
      }
   }   
   // if not char1 present return empty string 
   if(i == len){
      return "";
   }
   int start = i; // variable to store the starting index 
   bool isFound = false; // variable to mark whether char2 is found or not 
   int lastIdx = i-1; // variable to store last index of char3 
    
   // again traversing over the array from next index of i
   i++;
   for(; i<len; i++){
      if(str[i] == char2){
         isFound = true;
      } else if((str[i] == char3) and (isFound)){
         lastIdx = i;
      }
   }
   // return the final string 
   return str.substr(start, lastIdx-start + 1);
}
int main(){
   string str = "thisisthestring";
   char char1 = 'i';
   char char2 = 'e';
   char char3 = 'r';
   // calling the function to get the answer 
   string ans = getString(str, char1, char2, char3);
   if(ans == ""){
      // if the returned value is empty
      cout<<"The given string does not contain any substring with given characters present in the sequence"<<endl;
   } else {
      cout<<"The length of the required string is: "<<ans.length()<<" and the string is: "<<ans<<endl;
   }
   return 0;
}

Output

The length of the required string is: 10 and the string is: isisthestr

Time and Space Complexity

The time complexity of the above code is O(N) which is linear as we are only traversing over the array just once.

As we are not using any extra space makes the space complexity of the above code is constant or O(1).

Conclusion

In this tutorial, we have implemented a program to find the length of the longest substring from the given string that starts with the given character and ends with another different character. Also, it must include a third given character in between of it. We have implemented a program using for loop in the linear time complexity and constant extra space.

Updated on: 24-Aug-2023

30 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements