📌  相关文章
📜  没有相邻字符对的最长子串是相邻的英文字母

📅  最后修改于: 2021-09-07 02:19:46             🧑  作者: Mango

给定一个由小写英文字母组成的字符串S ,任务是从给定的字符串找到最长的子字符串,使得没有两个相邻的字符是相邻的英文字母。

例子:

处理方法:按照以下步骤解决问题:

  1. 初始化一个空字符串,比如T ,以在迭代期间存储所有临时子字符串。
  2. 初始化另一个字符串,例如ans ,以根据给定条件存储最长子字符串,并初始化一个变量,例如len ,以存储最长子字符串的长度。
  3. 将字符串的第一个字符附加到ans
  4. 遍历索引[1, S.length()]范围内的字符串并执行以下操作:
    • 如果相邻字符的ASCII 值之间的绝对差为1 ,则更新ans和最长字符串的长度,并将T设置为下一次迭代的当前字符。
    • 否则,将当前字符附加到字符串T
  5. 再次检查最长的子字符串并相应地更新值。
  6. 打印在变量ans 中获得的最长子字符串。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
 
#include 
using namespace std;
 
// Function to find the longest substring
// satisfying the given condition
void findSubstring(string S)
{
    // Stores all temporary substrings
    string T = "";
 
    // Stores the longest substring
    string ans = "";
 
    // Stores the length
    // of the substring T
    int l = 0;
 
    // Stores the first
    // character of string S
    T += S[0];
 
    // Traverse the string
    for (int i = 1; i < S.length(); i++) {
 
        // If the absolute difference is 1
        if (abs(S[i] - S[i - 1]) == 1) {
 
            // Update the length of
            // substring T
            l = T.length();
 
            // Update the longest substring
            if (l > ans.length()) {
                ans = T;
            }
 
            T = "";
            T += S[i];
        }
 
        // Otherwise, stores the current
        // character
        else {
            T += S[i];
        }
    }
 
    // Again checking for
    // longest substring
    // and update accordingly
    l = (int)T.length();
 
    if (l > (int)ans.length()) {
        ans = T;
    }
 
    // Print the longest substring
    cout << ans << endl;
}
 
// Driver Code
int main()
{
    // Given string
    string S = "aabdml";
 
    // Function call to find
    // the longest substring
    // satisfying given condition
    findSubstring(S);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
  // Function to find the longest substring
  // satisfying the given condition
  static void findSubstring(String S)
  {
 
    // Stores all temporary substrings
    String T = "";
 
    // Stores the longest substring
    String ans = "";
 
    // Stores the length
    // of the substring T
    int l = 0;
 
    // Stores the first
    // character of string S
    T += S.charAt(0);
 
    // Traverse the string
    for (int i = 1; i < S.length(); i++) {
 
      // If the absolute difference is 1
      if (Math.abs(S.charAt(i) - S.charAt(i - 1))
          == 1) {
 
        // Update the length of
        // substring T
        l = T.length();
 
        // Update the longest substring
        if (l > ans.length()) {
          ans = T;
        }
 
        T = "";
        T += S.charAt(i);
      }
 
      // Otherwise, stores the current
      // character
      else {
        T += S.charAt(i);
      }
    }
 
    // Again checking for
    // longest substring
    // and update accordingly
    l = T.length();
    if (l > ans.length()) {
      ans = T;
    }
 
    // Print the longest substring
    System.out.println(ans);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // Given string
    String S = "aabdml";
 
    // Function call to find
    // the longest substring
    // satisfying given condition
    findSubstring(S);
  }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 program for
# the above approach
 
# Function to find the longest substring
# satisfying the given condition
def findSubstring(S):
   
    # Stores all temporary substrings
    T = ""
 
    # Stores the longest substring
    ans = ""
 
    # Stores the length
    # of the subT
    l = 0
 
    # Stores the first
    # character of S
    T += S[0]
 
    # Traverse the string
    for i in range(1,len(S)):
 
        # If the absolute difference is 1
        if (abs(ord(S[i]) - ord(S[i - 1])) == 1):
 
            # Update the length of
            # subT
            l = len(T)
 
            # Update the longest substring
            if (l > len(ans)):
                ans = T
            T = ""
            T += S[i]
 
        # Otherwise, stores the current
        # character
        else:
            T += S[i]
 
    # Again checking for
    # longest substring
    # and update accordingly
    l = len(T)
    if (l > len(ans)):
        ans = T
 
    # Print the longest substring
    print (ans)
 
# Driver Code
if __name__ == '__main__':
   
    # Given string
    S = "aabdml"
 
    # Function call to find
    # the longest substring
    # satisfying given condition
    findSubstring(S)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
public class GFG
{
   
// Function to find the longest substring
// satisfying the given condition
static void findSubstring(string S)
{
   
    // Stores all temporary substrings
    string T = "";
 
    // Stores the longest substring
    string ans = "";
 
    // Stores the length
    // of the substring T
    int l = 0;
 
    // Stores the first
    // character of string S
    T += S[0];
 
    // Traverse the string
    for (int i = 1; i < S.Length; i++)
    {
 
        // If the absolute difference is 1
        if (Math.Abs(S[i] - S[i - 1]) == 1)
        {
 
            // Update the length of
            // substring T
            l = T.Length;
 
            // Update the longest substring
            if (l > ans.Length)
            {
                ans = T;
            }
 
            T = "";
            T += S[i];
        }
 
        // Otherwise, stores the current
        // character
        else
        {
            T += S[i];
        }
    }
 
    // Again checking for
    // longest substring
    // and update accordingly
    l = T.Length;
    if (l > ans.Length)
    {
        ans = T;
    }
 
    // Print the longest substring
    Console.Write(ans);
}
 
 
// Driver Code
public static void Main(String[] args)
{
   
    // Given string
    string S = "aabdml";
 
    // Function call to find
    // the longest substring
    // satisfying given condition
    findSubstring(S);
}
}
 
// This code is contributed by code_hunt.


Javascript


输出:
bdm

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

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