Given a string S consisting of lowercase English alphabets, the task is to find the longest substring from the given string such that no two adjacent characters are neighbouring English alphabets.
Examples:
Input: S = “aabdml”
Output: “bdm”
Explanation: Substring “bdm” is the longest substring which satisfies the given condition.
Input: S = “abc”
Output: “a”
Explanation: Substrings “a”, “b”, “c” satisfies the given condition. Print any one of them.
Approach: Follow the steps below to solve the problem:
- Initialize an empty string, say T, to store all the temporary substrings during iteration.
- Initialize another string, say ans, to store the longest substring as per the given conditions and a variable, say len, to store the length of the longest substring.
- Append the first character of the string to ans.
- Traverse the string in the range of indices [1, S.length()] and perform the following operations:
- If the absolute difference between ASCII values of the adjacent characters is 1, then update ans and the length of the longest string and set T equal to the current character for the next iteration.
- Otherwise, append the current character to string T.
- Check again for the longest substring and update the values accordingly.
- Print the longest substring obtained in variable ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findSubstring(string S)
{
string T = "" ;
string ans = "" ;
int l = 0;
T += S[0];
for ( int i = 1; i < S.length(); i++) {
if ( abs (S[i] - S[i - 1]) == 1) {
l = T.length();
if (l > ans.length()) {
ans = T;
}
T = "" ;
T += S[i];
}
else {
T += S[i];
}
}
l = ( int )T.length();
if (l > ( int )ans.length()) {
ans = T;
}
cout << ans << endl;
}
int main()
{
string S = "aabdml" ;
findSubstring(S);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findSubstring(String S)
{
String T = "" ;
String ans = "" ;
int l = 0 ;
T += S.charAt( 0 );
for ( int i = 1 ; i < S.length(); i++) {
if (Math.abs(S.charAt(i) - S.charAt(i - 1 ))
== 1 ) {
l = T.length();
if (l > ans.length()) {
ans = T;
}
T = "" ;
T += S.charAt(i);
}
else {
T += S.charAt(i);
}
}
l = T.length();
if (l > ans.length()) {
ans = T;
}
System.out.println(ans);
}
public static void main(String[] args)
{
String S = "aabdml" ;
findSubstring(S);
}
}
|
Python3
def findSubstring(S):
T = ""
ans = ""
l = 0
T + = S[ 0 ]
for i in range ( 1 , len (S)):
if ( abs ( ord (S[i]) - ord (S[i - 1 ])) = = 1 ):
l = len (T)
if (l > len (ans)):
ans = T
T = ""
T + = S[i]
else :
T + = S[i]
l = len (T)
if (l > len (ans)):
ans = T
print (ans)
if __name__ = = '__main__' :
S = "aabdml"
findSubstring(S)
|
C#
using System;
public class GFG
{
static void findSubstring( string S)
{
string T = "" ;
string ans = "" ;
int l = 0;
T += S[0];
for ( int i = 1; i < S.Length; i++)
{
if (Math.Abs(S[i] - S[i - 1]) == 1)
{
l = T.Length;
if (l > ans.Length)
{
ans = T;
}
T = "" ;
T += S[i];
}
else
{
T += S[i];
}
}
l = T.Length;
if (l > ans.Length)
{
ans = T;
}
Console.Write(ans);
}
public static void Main(String[] args)
{
string S = "aabdml" ;
findSubstring(S);
}
}
|
Javascript
<script>
function findSubstring(S) {
var T = "" ;
var ans = "" ;
var l = 0;
T += S[0];
for ( var i = 1; i < S.length; i++) {
if (Math.abs(S[i].charCodeAt(0) -
S[i - 1].charCodeAt(0)) === 1)
{
l = T.length;
if (l > ans.length) {
ans = T;
}
T = "" ;
T += S[i];
}
else {
T += S[i];
}
}
l = T.length;
if (l > ans.length) {
ans = T;
}
document.write(ans);
}
var S = "aabdml" ;
findSubstring(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
18 May, 2021
Like Article
Save Article