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

📅  最后修改于: 2021-04-17 14:29:23             🧑  作者: 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.


输出:
bdm

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