📜  以X开头和Y结束的最长子字符串

📅  最后修改于: 2021-04-21 22:31:52             🧑  作者: 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


输出:
12

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