- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Longest substring with no pair of adjacent characters are adjacent English alphabets
In the realm of string manipulation, identifying patterns and extracting meaningful substrings are common tasks. One intriguing problem involves finding the longest substring where no adjacent characters are adjacent English alphabets. In this article, we'll delve into an efficient solution to this problem using C++, along with a clear explanation and an example test case.
Problem Statement
Given a string of lowercase English alphabets, we need to find the length of the longest substring where no adjacent characters are adjacent English alphabets. For example, in the string "abacabx", the longest substring satisfying this condition is "abx", with a length of 3.
Approach and Algorithm
To solve this problem, we can utilize a greedy approach. We'll iterate through the given string and check if the current character and the previous character are adjacent English alphabets. If they are, we'll start a new substring. Otherwise, we'll extend the existing substring. By updating the length of the longest substring whenever it exceeds the previous maximum, we can find the desired result.
Implementation in C++
Here's 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; }
Output
Longest substring length: 3
Explanation of the Code
The function findLongestSubstring takes the input string as a parameter and returns the length of the longest substring with no adjacent English alphabet characters.
We initialize maxLength and currentLength to 0 and 1, respectively. We then iterate through the string starting from the second character. If the absolute difference between the current character and the previous character is not equal to 1, we increment currentLength to extend the current substring. Otherwise, we update maxLength if the current length exceeds the previous maximum and reset currentLength to 1.
Finally, we return the maximum length found. In the main function, we provide an example 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.
Input
string inputString = "abacabx";
In this case, the longest substring with no adjacent English alphabet characters is "abx" with a length of 3.
Conclusion
By employing a simple and efficient approach, we have successfully solved the problem of finding the longest substring with no adjacent English alphabet characters using C++. Understanding the provided code and the explanation will enable you to tackle similar problems involving string manipulation.