Check if a substring exists having only 2 distinct characters with frequency of one as twice the others
Last Updated :
12 Dec, 2022
Given a string str[] of N lower case English alphabets, the task is to check if there exists a substring of the given string such that the substring is composed of only two characters and the frequency of 1st character = 2 * frequency of 2nd character.
Example:
Input: str[] = “aaaabbc”
Output: Yes
Explanation: The substring str[0… 5] = “aaaabb” of the given string is composed of only two characters ‘a’ and ‘b’ and frequency of ‘a’ = 2 * frequency of ‘b’, i.e, 4 = 2 * 2. Another example of a valid substring is str[4… 6] = “bbc”.
Input: str[] = “abcdefg”
Output: No
Naive Approach: The given problem can be solved by iterating over all the substrings of the given string and checking whether the string is made up of only two characters and the frequency of the 1st character = 2 * frequency of the 2nd character.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using a Greedy Technique. Upon observing carefully, it can be noticed that for any substring to be valid, there must exist a substring of size 3 of that string such that it is made up of only two characters and the frequency of the 1st character = 2 * frequency of the 2nd character. Hence, the given problem can be solved by iterating through the given string and for each substring of length 3, check if there exists a string of the form “xxy“, “xyx“, or “yxx“.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible(string str)
{
for ( int i = 0; i + 2 < str.size(); i++) {
if (str[i] == str[i + 1]
&& str[i] != str[i + 2]) {
return true ;
}
if (str[i] == str[i + 2]
&& str[i] != str[i + 1]) {
return true ;
}
if (str[i + 1] == str[i + 2]
&& str[i] != str[i + 1]) {
return true ;
}
}
return false ;
}
int main()
{
string str = "aaaabbc" ;
if (isPossible(str))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
public class GFG {
public static boolean isPossible(String str)
{
for ( int i = 0 ; i + 2 < str.length(); i++) {
if (str.charAt(i) == str.charAt(i + 1 )
&& str.charAt(i) != str.charAt(i + 2 )) {
return true ;
}
if (str.charAt(i) == str.charAt(i + 2 )
&& str.charAt(i) != str.charAt(i + 1 )) {
return true ;
}
if (str.charAt(i + 1 ) == str.charAt(i + 2 )
&& str.charAt(i) != str.charAt(i + 1 )) {
return true ;
}
}
return false ;
}
public static void main(String args[]) {
String str = "aaaabbc" ;
if (isPossible(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isPossible( str ):
for i in range ( 0 , len ( str ) - 2 ):
if ( str [i] = = str [i + 1 ] and
str [i] ! = str [i + 2 ]):
return True
if ( str [i] = = str [i + 2 ] and
str [i] ! = str [i + 1 ]):
return True
if ( str [i + 1 ] = = str [i + 2 ] and
str [i] ! = str [i + 1 ]):
return True
return False
if __name__ = = "__main__" :
str = "aaaabbc"
if (isPossible( str )):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
public static bool isPossible(String str)
{
for ( int i = 0; i + 2 < str.Length; i++)
{
if (str[i] == str[i + 1]
&& str[i] != str[i + 2])
{
return true ;
}
if (str[i] == str[i + 2]
&& str[i] != str[i + 1])
{
return true ;
}
if (str[i + 1] == str[i + 2]
&& str[i] != str[i + 1])
{
return true ;
}
}
return false ;
}
public static void Main()
{
String str = "aaaabbc" ;
if (isPossible(str))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isPossible( str)
{
for (let i = 0; i + 2 < str.length; i++) {
if (str[i] == str[i + 1]
&& str[i] != str[i + 2]) {
return true ;
}
if (str[i] == str[i + 2]
&& str[i] != str[i + 1]) {
return true ;
}
if (str[i + 1] == str[i + 2]
&& str[i] != str[i + 1]) {
return true ;
}
}
return false ;
}
let str = "aaaabbc" ;
if (isPossible(str))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...