Check if a string follows a^nb^n pattern or not
Given string str, return true string follows pattern anbn, i.e., it has a’s followed by b’s such that the number of a’s and b’s are same.
Examples:
Input : str = "aabb"
Output : Yes
Input : str = "abab"
Output : No
Input : str = "aabbb"
Output : No
The idea is to first count a’s. If number of a’s is not equal to half of string’s length, then return false. Else check if all remaining characters are b’s or not.
Below is the implementation of the above idea :
C++
#include <iostream>
using namespace std;
bool isAnBn(string str)
{
int n = str.length();
int i;
for (i = 0; i < n; i++)
if (str[i] != 'a' )
break ;
if (i * 2 != n)
return false ;
int j;
for (j = i; j < n; j++)
if (str[j] != 'b' )
return false ;
return true ;
}
int main()
{
string str = "abab" ;
isAnBn(str) ? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class CheckPattern {
public static boolean isAnBn(String s)
{
int l = s.length();
if (l % 2 == 1 ) {
return false ;
}
int i = 0 ;
int j = l - 1 ;
while (i < j) {
if (s.charAt(i) != 'a' || s.charAt(j) != 'b' ) {
return false ;
}
i++;
j--;
}
return true ;
}
public static void main(String[] args)
throws java.lang.Exception
{
String s = "abab" ;
boolean value = isAnBn(s);
if (value == true ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
def isAnBn( str ):
n = len ( str )
for i in range (n):
if ( str [i] ! = 'a' ):
break
if (i * 2 ! = n):
return False
for j in range (i, n):
if ( str [j] ! = 'b' ):
return False
return True
if __name__ = = "__main__" :
str = "abab"
print ( "Yes" ) if isAnBn( str ) else print ( "No" )
|
C#
using System;
class GFG {
public static bool isAnBn(String s)
{
int l = s.Length;
if (l % 2 == 1) {
return false ;
}
int i = 0;
int j = l - 1;
while (i < j) {
if (s[i] != 'a' || s[j] != 'b' ) {
return false ;
}
i++;
j--;
}
return true ;
}
public static void Main()
{
String s = "abab" ;
bool value = isAnBn(s);
if (value == true ) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
}
|
PHP
<?php
function isAnBn( $str )
{
$n = strlen ( $str );
$i ;
for ( $i = 0; $i < $n ; $i ++)
if ( $str [ $i ] != 'a' )
break ;
if ( $i * 2 != $n )
return false;
$j ;
for ( $j = $i ; $j < $n ; $j ++)
if ( $str [ $j ] != 'b' )
return false;
return true;
}
$str = "abab" ;
if (isAnBn( $str ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isAnBn(s)
{
let l = s.length;
if (l % 2 == 1) {
return false ;
}
let i = 0;
let j = l - 1;
while (i < j) {
if (s[i] != 'a' || s[j] != 'b' ) {
return false ;
}
i++;
j--;
}
return true ;
}
let s = "abab" ;
let value = isAnBn(s);
if (value == true ) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Another approach:
The idea is to check element from first and last if at any stage our condition is not satisfied then return false.
Below is the implementation of the above code:
C++
#include <iostream>
using namespace std;
string isAnBn(string str)
{
int n = str.length();
if (n & 1)
return "No" ;
int i;
for (i = 0; i < n / 2; i++)
if (str[i] != 'a' || str[n - i - 1] != 'b' )
return "No" ;
return "Yes" ;
}
int main()
{
string str = "ab" ;
cout << isAnBn(str);
return 0;
}
|
Java
import java.io.*;
class GFG {
static String isAnBn(String str)
{
int n = str.length();
if ((n & 1 ) != 0 )
return "No" ;
int i;
for (i = 0 ; i < n / 2 ; i++)
if (str.charAt(i) != 'a' || str.charAt(n - i - 1 ) != 'b' )
return "No" ;
return "Yes" ;
}
public static void main (String[] args)
{
String str = "ab" ;
System.out.println(isAnBn(str));
}
}
|
Python3
def isanbn( str ):
n = len ( str )
if n& 1 :
return "No"
for i in range ( int (n / 2 )):
if str [i]! = 'a' or str [n - i - 1 ]! = 'b' :
return "No"
return "Yes"
input_str = "ab"
print (isanbn(input_str))
|
C#
using System;
public class GFG
{
static string isAnBn( string str)
{
int n = str.Length;
if ((n & 1) != 0)
return "No" ;
int i;
for (i = 0; i < n / 2; i++)
if (str[i] != 'a' || str[n-i-1] != 'b' )
return "No" ;
return "Yes" ;
}
static public void Main ()
{
string str = "ab" ;
Console.WriteLine(isAnBn(str));
}
}
|
Javascript
<script>
function isAnBn(str)
{
let n = str.length;
if ((n & 1) != 0)
return "No" ;
let i;
for (i = 0; i < n / 2; i++)
if (str[i] != 'a' || str[n - i - 1] != 'b' )
return "No" ;
return "Yes" ;
}
let str = "ab" ;
document.write(isAnBn(str));
</script>
|
Time complexity : O(n)
Auxiliary Space : O(1)
Last Updated :
11 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...