Home > Backend Development > C++ > body text

The longest substring in which adjacent characters do not have adjacent English letters

PHPz
Release: 2023-09-11 15:49:02
forward
789 people have browsed it

The longest substring in which adjacent characters do not have adjacent English letters

In the field of string manipulation, identifying patterns and extracting meaningful substrings are common tasks. An interesting problem involves finding the longest substring in which no adjacent characters are adjacent English letters. In this article, we will delve into an efficient solution to this problem using C and provide clear explanations and sample test cases.

Problem Statement

Given a string of lowercase English letters, we need to find the length of the longest substring in which no adjacent characters are adjacent English letters. For example, in the string "abacabx", the longest substring that meets this condition is "abx", with a length of 3.

Methods and Algorithms

In order to solve this problem, we can use the greedy method. We will iterate over the given string and check if the current character and the previous character are adjacent English letters. If it is, we start a new substring. Otherwise, we expand the existing substring. By updating the longest substring every time its length exceeds the previous maximum, we can find the desired result.

C implementation

This is the C code that solves the problem &minus,

#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;
}
Copy after login

Output

Longest substring length: 3
Copy after login

Code explanation

The function findLongestSubstring accepts an input string as a parameter and returns the length of the longest substring of non-adjacent English alphabetic characters.

We initialize maxLength and currentLength to 0 and 1 respectively. Then we iterate over the string starting from the second character. If the absolute difference between the current character and the previous character is not equal to 1, currentLength is increased to extend the current substring. Otherwise, if the current length exceeds the previous maximum, we update maxLength and reset currentLength to 1.

Finally, we return the maximum length found. In the main function, we provide a sample input string "abacabx" and print the length of the longest substring.

Example test case

Let's consider the example string "abacabx" to demonstrate our solution.

enter

string inputString = "abacabx";
Copy after login

In this example, the longest substring without adjacent English alphabetic characters is "abx", with a length of 3.

in conclusion

By adopting a simple and efficient method, we successfully solved the problem of finding the longest non-adjacent English letter substring using C. Understanding the code and explanations provided will enable you to solve similar problems involving string manipulation.

The above is the detailed content of The longest substring in which adjacent characters do not have adjacent English letters. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!