查詢最長子字串,其中沒有相鄰的英文字母


在字串操作領域,識別模式和提取有意義的子字串是常見任務。一個有趣的問題涉及查詢最長的子字串,其中沒有相鄰的字元是相鄰的英文字母。在本文中,我們將深入探討解決此問題的有效解決方案,並提供清晰的解釋和示例測試用例。

問題陳述

給定一個由小寫英文字母組成的字串,我們需要找到最長子字串的長度,其中沒有相鄰的字元是相鄰的英文字母。例如,在字串“abacabx”中,滿足此條件的最長子字串是“abx”,長度為3。

方法和演算法

為了解決這個問題,我們可以利用貪心演算法。我們將遍歷給定的字串,並檢查當前字元和前一個字元是否為相鄰的英文字母。如果是,我們將開始一個新的子字串。否則,我們將擴充套件現有的子字串。透過在最長子字串的長度超過之前最大值時更新它,我們可以找到所需的結果。

實現

以下是解決問題的程式碼:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Function to find the longest substring with consecutive characters having a difference of 1 in their ASCII values
int findLongestSubstring(const char* str) {
   int maxLength = 0;
   int currentLength = 1;
    
   // Loop through the characters in the string
   for (int i = 1; i < strlen(str); i++) {
      // Check if the difference between adjacent characters is not equal to 1
      if (abs(str[i] - str[i - 1]) != 1) {
         currentLength++;  // Increment the current substring length
      } else {
         maxLength = (maxLength > currentLength) ? maxLength : currentLength;   // Update the maximum length if needed
         currentLength = 1;  // Reset the current substring length
      }
   }
   
   maxLength = (maxLength > currentLength) ? maxLength : currentLength;    // Check for the last substring
   return maxLength;  // Return the length of the longest substring
}

int main() {
   const char* inputString = "abacabx";
   int longestSubstringLength = findLongestSubstring(inputString);
   
   printf("Longest substring length: %d\n", longestSubstringLength);
   
   return 0;
}

輸出

Longest substring length: 3
#include <iostream>
#include <string>
using namespace std;

int findLongestSubstring(const string& str) {
   int maxLength = 0;
   int currentLength = 1;
   
   for (int i = 1; i < str.length(); i++) {
      if (abs(str[i] - str[i - 1]) != 1) {
         currentLength++;
      } else {
         maxLength = max(maxLength, currentLength);
         currentLength = 1;
      }
   }
   
   maxLength = max(maxLength, currentLength);
   return maxLength;
}

int main() {
   string inputString = "abacabx";
   int longestSubstringLength = findLongestSubstring(inputString);
   
   cout << "Longest substring length: " << longestSubstringLength << endl;
   
   return 0;
}

輸出

Longest substring length: 3
public class LongestSubstring {

   public static int findLongestSubstring(String str) {
      int maxLength = 0;
      int currentLength = 1;

      // Loop through the characters in the string
      for (int i = 1; i < str.length(); i++) {
         // Check if the difference between adjacent characters is not equal to 1
         if (Math.abs(str.charAt(i) - str.charAt(i - 1)) != 1) {
            currentLength++; // Increment the current substring length
         } else {
            maxLength = Math.max(maxLength, currentLength); // Update the maximum length if needed
            currentLength = 1; // Reset the current substring length
         }
      }

      maxLength = Math.max(maxLength, currentLength); // Check for the last substring
      return maxLength; // Return the length of the longest substring
   }

   public static void main(String[] args) {
      String inputString = "abacabx";
      int longestSubstringLength = findLongestSubstring(inputString);

      System.out.println("Longest substring length: " + longestSubstringLength);
   }
}

輸出

Longest substring length: 3
def find_longest_substring(s):
   max_length = 0
   current_length = 1

   # Loop through the characters in the string
   for i in range(1, len(s)):
      # Check if the difference between adjacent characters is not equal to 1
      if abs(ord(s[i]) - ord(s[i - 1])) != 1:
         current_length += 1 # Increment the current substring length
      else:
         max_length = max(max_length, current_length) # Update the maximum length if needed
         current_length = 1 # Reset the current substring length

   max_length = max(max_length, current_length) # Check for the last substring
   return max_length # Return the length of the longest substring

def main():
   input_string = "abacabx"
   longest_substring_length = find_longest_substring(input_string)

   print("Longest substring length:", longest_substring_length)

if __name__ == "__main__":
   main()

輸出

Longest substring length: 3

程式碼解釋

函式 findLongestSubstring 將輸入字串作為引數,並返回沒有相鄰的英文字母字元的最長子字串的長度。

我們分別將 maxLength 和 currentLength 初始化為 0 和 1。然後,我們從第二個字元開始遍歷字串。如果當前字元和前一個字元之間的絕對差值不等於 1,我們將增加 currentLength 以擴充套件當前子字串。否則,如果當前長度超過之前最大值,我們將更新 maxLength 並將 currentLength 重置為 1。

最後,我們返回找到的最大長度。在主函式中,我們提供了一個示例輸入字串“abacabx”並列印最長子字串的長度。

示例測試用例

讓我們考慮示例字串“abacabx”來演示我們的解決方案。

string inputString = "abacabx";

在這種情況下,沒有相鄰的英文字母字元的最長子字串是“abx”,長度為3。

結論

透過採用簡單有效的方法,我們成功地解決了使用 C++ 查詢沒有相鄰的英文字母字元的最長子字串的問題。瞭解提供的程式碼和解釋將使您能夠解決涉及字串操作的類似問題。

更新於: 2023年10月23日

316 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告