📜  包含 C2 的最长子串,以 C1 开始,以 C3 结束

📅  最后修改于: 2022-05-13 01:56:07.602000             🧑  作者: Mango

包含 C2 的最长子串,以 C1 开始,以 C3 结束

给定一个字符串S 。查找以字符C1开头、以字符C3结尾并且其间至少有一个字符C2的最长子字符串。如果不存在这样的子字符串,则打印“-1”

注意:大写和小写字母的处理方式不同,因此“a”“A”不等价。

例子:

方法:可以使用以下想法解决问题:

请按照以下步骤解决问题:

  • S中找到C1的第一次出现(比如i )。
  • i开始迭代到字符串的结尾:
    • 将字符附加到最长的子字符串。
    • 如果任何字符与C2C2递增计数相同。
    • 如果找到C3并且C2的计数不为零,则更新最长的子字符串。
  • 如果计数为 0,则返回 -1。
  • 否则,返回最后一次出现C3之前形成的子字符串。

下面是上述方法的实现。

C++14
// C++ code to implement the approach
#include 
using namespace std;
 
// Function to find the longest substring
string Solve(string& S, char& C1,
             char& C2, char& C3)
{
    int maxLen = 0, countB = 0,
        i, j, n = S.length();
 
    // First occurrence of C1
    for (i = 0; i < n; i++) {
        if (S[i] == C1) {
            j = i;
            break;
        }
    }
 
    // Finding the longest substring
    while (j++ < n) {
        if (S[j] == C2)
            countB++;
 
        if (countB > 0 && S[j] == C3)
            maxLen = max(maxLen, j - i + 1);
    }
 
    if (maxLen == 0)
        return "-1";
 
    return S.substr(i, maxLen);
}
 
// Driver code
int main()
{
    string S = "GeeksForGeeks";
    char C1 = 'G';
    char C2 = 'e';
    char C3 = 'k';
 
    string ans = Solve(S, C1, C2, C3);
 
    if (ans.compare("-1") == 0)
        cout << "-1";
    else
        cout << ans.length() << " "
             << ans;
    return 0;
}


Java
// Java code to implement the approach
public class GFG
{
   
    // Function to find the longest substring
    static String Solve(String S, char C1, char C2, char C3)
    {
        int maxLen = 0;
        int countB = 0;
        int i, j = 0;
        int n = S.length();
       
        // First occurrence of C1
        for (i = 0; i < n; i++) {
            if (S.charAt(i) == C1) {
                j = i;
                break;
            }
        }
       
        // Finding the longest substring
        while (++j < n) {
            if (S.charAt(j) == C2)
                countB++;
 
            if (countB > 0 && S.charAt(j) == C3)
                maxLen = Math.max(maxLen, j - i + 1);
        }
        if (maxLen == 0)
            return "-1";
        return S.substring(i, maxLen);
    }
   
    // Driver code
    public static void main(String[] args)
    {
        String S = "GeeksForGeeks";
        char C1 = 'G';
        char C2 = 'e';
        char C3 = 'k';
 
        String ans = Solve(S, C1, C2, C3);
        if (ans == "-1")
            System.out.println("-1");
        else
            System.out.println(ans.length() + " " + ans);
    }
}
 
//This code is contributed by phasing17


Python3
# Python 3 code to implement the approach
 
# Function to find the longest substrin
def Solve(S,  C1,
          C2, C3):
 
    maxLen = 0
    countB = 0
    n = len(S)
 
    # First occurrence of C1
    for i in range(n):
        if (S[i] == C1):
            j = i
            break
 
    # Finding the longest substring
    while (j < n):
        if (S[j] == C2):
            countB += 1
 
        if (countB > 0 and S[j] == C3):
            maxLen = max(maxLen, j - i + 1)
        j += 1
 
    if (maxLen == 0):
        return "-1"
 
    return S[i: maxLen]
 
 
# Driver code
if __name__ == "__main__":
 
    S = "GeeksForGeeks"
    C1 = 'G'
    C2 = 'e'
    C3 = 'k'
 
    ans = Solve(S, C1, C2, C3)
 
    if (ans == "-1"):
        print("-1")
    else:
        print(len(ans), ans)
 
        # This code is contributed by ukasp.


C#
// C# code to implement the approach
using System;
class GeeksForGeeks {
 
    // Function to find the longest substring
    static string Solve(string S, char C1, char C2, char C3)
    {
        int maxLen = 0, countB = 0, i = 0, j = 0,
            n = S.Length;
 
        // First occurrence of C1
        for (i = 0; i < n; i++) {
            if (S[i] == C1) {
                j = i;
                break;
            }
        }
 
        // Finding the longest substring
        while (j < n) {
            if (S[j] == C2)
                countB++;
 
            if (countB > 0 && S[j] == C3)
                maxLen = Math.Max(maxLen, j - i + 1);
 
            j++;
        }
 
        if (maxLen == 0)
            return "-1";
 
        return S.Substring(i, maxLen);
    }
 
    // Driver code
    public static void Main()
    {
        string S = "GeeksForGeeks";
        char C1 = 'G';
        char C2 = 'e';
        char C3 = 'k';
 
        string ans = Solve(S, C1, C2, C3);
 
        if (ans == "-1")
            Console.Write("-1");
        else
            Console.Write(ans.Length + " " + ans);
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
12 GeeksForGeek

时间复杂度: O(N),其中 N 是字符串的长度
辅助空间: O(1)