📜  以 X 开头并以 Y 结尾的最长子串

📅  最后修改于: 2021-09-04 07:47:17             🧑  作者: Mango

给定一个字符串str ,两个字符XY 。任务是找到以X开头并以Y结尾的最长子串的长度。假设总是存在一个以X开头并以Y结尾的子串。
例子:

朴素的方法:朴素的方法是找到给定字符串的所有子字符串,从中找到以X开头并以Y结尾的最大子字符串。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:为了优化上述方法, XY之间的字符数应该是最大的。因此,使用指针startend遍历字符串以从起始索引中找到第一次出现的X和从结尾处最后一次出现的Y。以下是步骤:

  1. 初始化start = 0end =字符串长度 – 1
  2. 从头开始遍历字符串并找到第一个出现的字符X 。让它在索引xPos 处
  3. 从头开始遍历字符串并找到最后一次出现的字符Y 。让它在索引yPos 处
  4. 最长子串的长度由(yPos – xPos + 1) 给出

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function returns length of longest
// substring starting with X and
// ending with Y
int longestSubstring(string str,
                    char X, char Y)
{
    // Length of string
    int N = str.length();
    int start = 0;
    int end = N - 1;
    int xPos = 0;
    int yPos = 0;
 
    // Find the length of the string
    // starting with X from the beginning
    while (true) {
 
        if (str[start] == X) {
            xPos = start;
            break;
        }
        start++;
    }
 
    // Find the length of the string
    // ending with Y from the end
    while (true) {
 
        if (str[end] == Y) {
            yPos = end;
            break;
        }
        end--;
    }
 
    // Longest substring
    int length = (yPos - xPos) + 1;
 
    // Print the length
    cout << length;
}
 
// Driver Code
int main()
{
    // Given string str
    string str = "HASFJGHOGAKZZFEGA";
 
    // Starting and Ending characters
    char X = 'A', Y = 'Z';
 
    // Function Call
    longestSubstring(str, X, Y);
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function returns length of longest
// substring starting with X and
// ending with Y
public static void longestSubstring(String str,
                                    char X, char Y)
{
     
    // Length of string
    int N = str.length();
    int start = 0;
    int end = N - 1;
    int xPos = 0;
    int yPos = 0;
     
    // Find the length of the string
    // starting with X from the beginning
    while (true)
    {
        if (str.charAt(start) == X)
        {
            xPos = start;
            break;
        }
        start++;
    }
     
    // Find the length of the string
    // ending with Y from the end
    while (true)
    {
        if (str.charAt(end) == Y)
        {
            yPos = end;
            break;
        }
        end--;
    }
     
    // Longest substring
    int length = (yPos - xPos) + 1;
     
    // Print the length
    System.out.print(length);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given string str
    String str = "HASFJGHOGAKZZFEGA";
     
    // Starting and Ending characters
    char X = 'A', Y = 'Z';
     
    // Function Call
    longestSubstring(str, X, Y);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
 
# Function returns length of longest
# substring starting with X and
# ending with Y
def longestSubstring(str, X, Y):
     
    # Length of string
    N = len(str)
     
    start = 0
    end = N - 1
    xPos = 0
    yPos = 0
 
    # Find the length of the string
    # starting with X from the beginning
    while (True):
        if (str[start] == X):
            xPos = start
            break
             
        start += 1
 
    # Find the length of the string
    # ending with Y from the end
    while (True):
        if (str[end] == Y):
            yPos = end
            break
         
        end -= 1
 
    # Longest substring
    length = (yPos - xPos) + 1
 
    # Print the length
    print(length)
 
# Driver Code
if __name__ == "__main__":
     
    # Given string str
    str = "HASFJGHOGAKZZFEGA"
 
    # Starting and Ending characters
    X = 'A'
    Y = 'Z'
 
    # Function Call
    longestSubstring(str, X, Y)
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach 
using System;
 
class GFG{
     
// Function returns length of longest
// substring starting with X and
// ending with Y
static void longestSubstring(string str,
                             char X, char Y)
{
     
    // Length of string
    int N = str.Length;
    int start = 0;
    int end = N - 1;
    int xPos = 0;
    int yPos = 0;
     
    // Find the length of the string
    // starting with X from the beginning
    while (true)
    {
        if (str[start] == X)
        {
            xPos = start;
            break;
        }
        start++;
    }
     
    // Find the length of the string
    // ending with Y from the end
    while (true)
    {
        if (str[end] == Y)
        {
            yPos = end;
            break;
        }
        end--;
    }
     
    // Longest substring
    int length = (yPos - xPos) + 1;
     
    // Print the length
    Console.Write(length);
}
 
// Driver code
public static void Main()
{
     
    // Given string str
    string str = "HASFJGHOGAKZZFEGA";
     
    // Starting and Ending characters
    char X = 'A', Y = 'Z';
     
    // Function call
    longestSubstring(str, X, Y);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:

12

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live