Longest subsequence such that no 3 consecutive characters are same
Last Updated :
09 Mar, 2023
Given a string of lowercase characters S, the task is to find longest subsequence of the string with no 3 consecutive identical characters.
Examples:
Input: S = “eedaaad”
Output: eedaad
Explanation: One occurrence of letter a is deleted.
Input: xxxtxxx
Output: xxtxx
Approach: The task can be solved by checking every window of size 3. If any of the 3 characters mismatch, append it to the resultant string, else continue. At last, print the resultant string.
Effective Approach:
1. Define a static method named “filterString” that takes a string “s1” as input and returns a string.
2. Create a StringBuilder object “sb1” to store the filtered string.
3. Append the first two characters of “s1” to “sb1”.
4. Loop over the remaining characters of “s1” starting from the third character:
a. Check if the current character is different from the previous two characters.
b. If it is, append the current character to “sb1”.
5. Return the filtered string by calling the “toString” method on “sb1”.
6. In the main method:
a. Create a string “s” and assign it a value.
b. Call the “filterString” method with “s” as input and assign the result to “res”.
c. Print the value of “res”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string filterString(string s1)
{
string sb1 = "" ;
sb1 += s1[0];
sb1 += (s1[1]);
for ( int i = 2; i < s1.length(); ++i)
{
if (s1[i] != s1[i - 1] || s1[i] != s1[i - 2])
{
sb1 += s1[i];
}
}
return sb1;
}
int main()
{
string s = "eedaaad" ;
string res = filterString(s);
cout << (res);
return 0;
}
|
Java
class Solution {
public static String
filterString(String s1)
{
StringBuilder sb1 = new StringBuilder();
sb1.append(s1.charAt( 0 ));
sb1.append(s1.charAt( 1 ));
for ( int i = 2 ; i < s1.length();
++i) {
if (s1.charAt(i) != s1.charAt(i - 1 )
|| s1.charAt(i) != s1.charAt(i - 2 )) {
sb1.append(s1.charAt(i));
}
}
return sb1.toString();
}
public static void main(String[] args)
{
String s = "eedaaad" ;
String res = filterString(s);
System.out.println(res);
}
}
|
Python3
def filterString(s1):
sb1 = ""
sb1 + = s1[ 0 ]
sb1 + = (s1[ 1 ])
for i in range ( 2 , len (s1)):
if (s1[i] ! = s1[i - 1 ] or s1[i] ! = s1[i - 2 ]):
sb1 + = s1[i]
return sb1
if __name__ = = "__main__" :
s = "eedaaad"
res = filterString(s)
print (res)
|
C#
using System;
using System.Text;
class Solution
{
public static string filterstring( string s1)
{
StringBuilder sb1 = new StringBuilder();
sb1.Append(s1[0]);
sb1.Append(s1[1]);
for ( int i = 2; i < s1.Length; ++i)
{
if (s1[i] != s1[i - 1]
|| s1[i] != s1[i - 2])
{
sb1.Append(s1[i]);
}
}
return sb1.ToString();
}
public static void Main()
{
string s = "eedaaad" ;
string res = filterstring(s);
Console.Write(res);
}
}
|
Javascript
<script>
const filterString = (s1) => {
let sb1 = "" ;
sb1 += s1[0];
sb1 += (s1[1]);
for (let i = 2; i < s1.length; ++i) {
if (s1[i] != s1[i - 1] || s1[i] != s1[i - 2]) {
sb1 += s1[i];
}
}
return sb1;
}
let s = "eedaaad" ;
let res = filterString(s);
document.write(res);
</script>
|
Time Complexity: O(N), where N is the length of string
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...