Longest substring that starts with X and ends with Y
Given a string str, two characters X and Y. The task is to find the length of the longest substring that starts with X and ends with Y. It is given that there always exists a substring that starts with X and ends with Y.
Examples:
Input: str = “QWERTYASDFZXCV”, X = ‘A’, Y = ‘Z’
Output: 5
Explanation:
The largest substring which start with ‘A’ and end with ‘Z’ = “ASDFZ”.
Size of the substring = 5.
Input: str = “ZABCZ”, X = ‘Z’, Y = ‘Z’
Output: 3
Explanation:
The largest substring which start with ‘Z’ and end with ‘Z’ = “ZABCZ”.
Size of the substring = 5.
Naive Approach: The naive approach is to find all the substrings of the given string out of these find the largest substring which starts with X and ends with Y.
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubstring(string str, char X, char Y)
{
int n = str.size();
int ans = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (str[i] == X && str[j] == Y) {
ans = max(ans, j - i + 1);
}
}
}
return ans;
}
int main()
{
string str = "HASFJGHOGAKZZFEGA" ;
char X = 'A' , Y = 'Z' ;
cout << longestSubstring(str, X, Y) << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int longestSubstring(String str, char X,
char Y)
{
int n = str.length();
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
if (str.charAt(i) == X
&& str.charAt(j) == Y) {
ans = Math.max(ans, j - i + 1 );
}
}
}
return ans;
}
public static void main(String[] args)
{
String str = "HASFJGHOGAKZZFEGA" ;
char X = 'A' , Y = 'Z' ;
System.out.println(longestSubstring(str, X, Y));
}
}
|
Python3
def longest_substring( str , X, Y):
n = len ( str )
ans = 0
for i in range (n):
for j in range (i + 1 , n):
if str [i] = = X and str [j] = = Y:
ans = max (ans, j - i + 1 )
return ans
str = "HASFJGHOGAKZZFEGA"
X = 'A'
Y = 'Z'
print (longest_substring( str , X, Y))
|
C#
using System;
public class GFG {
public static int LongestSubstring( string str, char X,
char Y)
{
int n = str.Length;
int ans = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (str[i] == X && str[j] == Y) {
ans = Math.Max(ans, j - i + 1);
}
}
}
return ans;
}
public static void Main()
{
string str = "HASFJGHOGAKZZFEGA" ;
char X = 'A' , Y = 'Z' ;
Console.WriteLine(LongestSubstring(str, X, Y));
}
}
|
Javascript
function longest_substring(str, X, Y) {
let n = str.length;
let ans = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (str[i] == X && str[j] == Y) {
ans = Math.max(ans, j - i + 1);
}
}
}
return ans;
}
let str = "HASFJGHOGAKZZFEGA" ;
let X = 'A' ;
let Y = 'Z' ;
console.log(longest_substring(str, X, Y));
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimized the above approach, the count of characters between X and Y should be the largest. So, iterate over the string using pointers start and end to find the first occurrence of X from the starting index and the last occurrence of Y from the end. Below are the steps:
- Initialize start = 0 and end = length of string – 1.
- Traverse the string from the beginning and find the first occurrence of character X. Let it be at index xPos.
- Traverse the string from the beginning and find the last occurrence of character Y. Let it be at index yPos.
- The length of the longest substring is given by (yPos – xPos + 1).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubstring(string str,
char X, char Y)
{
int N = str.length();
int start = 0;
int end = N - 1;
int xPos = 0;
int yPos = 0;
while ( true ) {
if (str[start] == X) {
xPos = start;
break ;
}
start++;
}
while ( true ) {
if (str[end] == Y) {
yPos = end;
break ;
}
end--;
}
int length = (yPos - xPos) + 1;
cout << length;
}
int main()
{
string str = "HASFJGHOGAKZZFEGA" ;
char X = 'A' , Y = 'Z' ;
longestSubstring(str, X, Y);
return 0;
}
|
Java
class GFG{
public static void longestSubstring(String str,
char X, char Y)
{
int N = str.length();
int start = 0 ;
int end = N - 1 ;
int xPos = 0 ;
int yPos = 0 ;
while ( true )
{
if (str.charAt(start) == X)
{
xPos = start;
break ;
}
start++;
}
while ( true )
{
if (str.charAt(end) == Y)
{
yPos = end;
break ;
}
end--;
}
int length = (yPos - xPos) + 1 ;
System.out.print(length);
}
public static void main(String[] args)
{
String str = "HASFJGHOGAKZZFEGA" ;
char X = 'A' , Y = 'Z' ;
longestSubstring(str, X, Y);
}
}
|
Python3
def longestSubstring( str , X, Y):
N = len ( str )
start = 0
end = N - 1
xPos = 0
yPos = 0
while ( True ):
if ( str [start] = = X):
xPos = start
break
start + = 1
while ( True ):
if ( str [end] = = Y):
yPos = end
break
end - = 1
length = (yPos - xPos) + 1
print (length)
if __name__ = = "__main__" :
str = "HASFJGHOGAKZZFEGA"
X = 'A'
Y = 'Z'
longestSubstring( str , X, Y)
|
C#
using System;
class GFG{
static void longestSubstring( string str,
char X, char Y)
{
int N = str.Length;
int start = 0;
int end = N - 1;
int xPos = 0;
int yPos = 0;
while ( true )
{
if (str[start] == X)
{
xPos = start;
break ;
}
start++;
}
while ( true )
{
if (str[end] == Y)
{
yPos = end;
break ;
}
end--;
}
int length = (yPos - xPos) + 1;
Console.Write(length);
}
public static void Main()
{
string str = "HASFJGHOGAKZZFEGA" ;
char X = 'A' , Y = 'Z' ;
longestSubstring(str, X, Y);
}
}
|
Javascript
<script>
function longestSubstring(str, X, Y) {
var N = str.length;
var start = 0;
var end = N - 1;
var xPos = 0;
var yPos = 0;
while ( true ) {
if (str[start] === X) {
xPos = start;
break ;
}
start++;
}
while ( true ) {
if (str[end] === Y) {
yPos = end;
break ;
}
end--;
}
var length = yPos - xPos + 1;
document.write(length);
}
var str = "HASFJGHOGAKZZFEGA" ;
var X = "A" ,
Y = "Z" ;
longestSubstring(str, X, Y);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
14 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...