Check if both halves of the string have same set of characters
Given a string of lowercase characters only, the task is to check if it is possible to split a string from the middle which will give two halves having the same characters and same frequency of each character. If the length of the given string is ODD then ignore the middle element and check for the rest.
Examples:
Input: abbaab
Output: NO
The two halves contain the same characters
but their frequencies do not match so they
are NOT CORRECT
Input : abccab
Output : YES
Algorithm:
- Declare two counter arrays for keeping count of characters in two half of the string, each of size 26.
- Now run a loop and take two variables i and j, where i starts from 0 and j starts from (length of string – 1).
- For each character in the string, go to the corresponding index in the counter arrays and increment the value by 1 and increment i and decrement j. Do this until i is less than j.
- After finishing STEP 3, again run a loop and compare values of counter arrays. If value of first array if not equal to value of second array, then return false.
- If all counts matched, return true.
Below is the implementation of above idea:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
bool checkCorrectOrNot(string s)
{
int count1[MAX_CHAR] = {0};
int count2[MAX_CHAR] = {0};
int n = s.length();
if (n == 1)
return true ;
for ( int i=0,j=n-1; i<j; i++,j--)
{
count1[s[i]- 'a' ]++;
count2[s[j]- 'a' ]++;
}
for ( int i = 0; i<MAX_CHAR; i++)
if (count1[i] != count2[i])
return false ;
return true ;
}
int main()
{
string s = "abab" ;
if (checkCorrectOrNot(s))
cout << "Yes\n" ;
else
cout << "No\n" ;
return 0;
}
|
Java
public class GFG {
static final int MAX_CHAR = 26 ;
static boolean checkCorrectOrNot(String s)
{
int [] count1 = new int [MAX_CHAR];
int [] count2 = new int [MAX_CHAR];
int n = s.length();
if (n == 1 )
return true ;
for ( int i = 0 , j = n - 1 ; i < j; i++, j--)
{
count1[s.charAt(i) - 'a' ]++;
count2[s.charAt(j) - 'a' ]++;
}
for ( int i = 0 ; i < MAX_CHAR; i++)
if (count1[i] != count2[i])
return false ;
return true ;
}
public static void main(String args[])
{
String s = "abab" ;
if (checkCorrectOrNot(s))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
MAX_CHAR = 26
def checkCorrectOrNot(s):
global MAX_CHAR
count1 = [ 0 ] * MAX_CHAR
count2 = [ 0 ] * MAX_CHAR
n = len (s)
if n = = 1 :
return true
i = 0 ; j = n - 1
while (i < j):
count1[ ord (s[i]) - ord ( 'a' )] + = 1
count2[ ord (s[j]) - ord ( 'a' )] + = 1
i + = 1 ; j - = 1
for i in range (MAX_CHAR):
if count1[i] ! = count2[i]:
return False
return True
s = "ababc"
print ( "Yes" if checkCorrectOrNot(s) else "No" )
|
C#
using System;
class GFG {
static int MAX_CHAR = 26;
static bool checkCorrectOrNot( string s)
{
int []count1 = new int [MAX_CHAR];
int []count2 = new int [MAX_CHAR];
int n = s.Length;
if (n == 1)
return true ;
for ( int i = 0, j = n - 1; i < j;
i++, j--)
{
count1[s[i] - 'a' ]++;
count2[s[j] - 'a' ]++;
}
for ( int i = 0; i < MAX_CHAR; i++)
if (count1[i] != count2[i])
return false ;
return true ;
}
public static void Main()
{
string s = "abab" ;
if (checkCorrectOrNot(s))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
$MAX_CHAR = 26;
function checkCorrectOrNot( $s )
{
global $MAX_CHAR ;
$count1 = array_fill (0, $MAX_CHAR , NULL);
$count2 = array_fill (0, $MAX_CHAR , NULL);
$n = strlen ( $s );
if ( $n == 1)
return true;
for ( $i = 0, $j = $n - 1;
$i < $j ; $i ++, $j --)
{
$count1 [ $s [ $i ] - 'a' ]++;
$count2 [ $s [ $j ] - 'a' ]++;
}
for ( $i = 0; $i < $MAX_CHAR ; $i ++)
if ( $count1 [ $i ] != $count2 [ $i ])
return false;
return true;
}
$s = "abab" ;
if (checkCorrectOrNot( $s ))
echo "Yes\n" ;
else
echo "No\n" ;
?>
|
Javascript
<script>
let MAX_CHAR = 26;
function checkCorrectOrNot(s)
{
let count1 = new Array(MAX_CHAR);
let count2 = new Array(MAX_CHAR);
for (let i=0;i<MAX_CHAR;i++)
{
count1[i]=0;
count2[i]=0;
}
let n = s.length;
if (n == 1)
return true ;
for (let i = 0, j = n - 1; i < j; i++, j--)
{
count1[s[i] - 'a' ]++;
count2[s[j] - 'a' ]++;
}
for (let i = 0; i < MAX_CHAR; i++)
if (count1[i] != count2[i])
return false ;
return true ;
}
let s = "abab" ;
if (checkCorrectOrNot(s))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity : O(n), where n is the length of the string.
Auxiliary Space : O(1)
Space optimized solution:
Below is the space optimized solution of the above approach.
- We can solve this problem by using only 1 counter array.
- Take a string and increment counts for first half and then decrement counts for second half.
- If final counter array is 0, then return true, Else False.
Below is the implementation of above idea:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
bool checkCorrectOrNot(string s)
{
int count[MAX_CHAR] = {0};
int n = s.length();
if (n == 1)
return true ;
for ( int i=0,j=n-1; i<j; i++,j--)
{
count[s[i]- 'a' ]++;
count[s[j]- 'a' ]--;
}
for ( int i = 0; i<MAX_CHAR; i++)
if (count[i] != 0)
return false ;
return true ;
}
int main()
{
string s = "abab" ;
if (checkCorrectOrNot(s))
cout << "Yes\n" ;
else
cout << "No\n" ;
return 0;
}
|
Java
public class GFG {
static final int MAX_CHAR = 26 ;
static boolean checkCorrectOrNot(String s)
{
int [] count = new int [MAX_CHAR];
int n = s.length();
if (n == 1 )
return true ;
for ( int i = 0 ,j = n - 1 ; i < j; i++, j--)
{
count[s.charAt(i) - 'a' ]++;
count[s.charAt(j) - 'a' ]--;
}
for ( int i = 0 ; i < MAX_CHAR; i++)
if (count[i] != 0 )
return false ;
return true ;
}
public static void main(String args[])
{
String s = "abab" ;
if (checkCorrectOrNot(s))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
MAX_CHAR = 26
def checkCorrectOrNot(s):
global MAX_CHAR
count = [ 0 ] * MAX_CHAR
n = len (s)
if n = = 1 :
return true
i = 0 ; j = n - 1
while i < j:
count[ ord (s[i]) - ord ( 'a' )] + = 1
count[ ord (s[j]) - ord ( 'a' )] - = 1
i + = 1 ; j - = 1
for i in range (MAX_CHAR):
if count[i] ! = 0 :
return False
return True
s = "abab"
print ( "Yes" if checkCorrectOrNot(s) else "No" )
|
C#
using System;
public class GFG {
static int MAX_CHAR = 26;
static bool checkCorrectOrNot(String s)
{
int [] count = new int [MAX_CHAR];
int n = s.Length;
if (n == 1)
return true ;
for ( int i = 0, j = n - 1; i < j; i++, j--)
{
count[s[i] - 'a' ]++;
count[s[j] - 'a' ]--;
}
for ( int i = 0; i < MAX_CHAR; i++)
if (count[i] != 0)
return false ;
return true ;
}
public static void Main(String []args)
{
String s = "abab" ;
if (checkCorrectOrNot(s))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?PHP
$MAX_CHAR = 26;
function checkCorrectOrNot( $s )
{
global $MAX_CHAR ;
$count = array_fill (0, $MAX_CHAR , NULL);
$n = strlen ( $s );
if ( $n == 1)
return true;
for ( $i = 0, $j = $n - 1;
$i < $j ; $i ++, $j --)
{
$count [ $s [ $i ] - 'a' ]++;
$count [ $s [ $j ] - 'a' ]--;
}
for ( $i = 0; $i < $MAX_CHAR ; $i ++)
if ( $count [ $i ] != 0)
return false;
return true;
}
$s = "abab" ;
if (checkCorrectOrNot( $s ))
echo "Yes\n" ;
else
echo "No\n" ;
?>
|
Javascript
<script>
let MAX_CHAR = 26;
function checkCorrectOrNot(s)
{
let count = new Array(MAX_CHAR);
for (let i = 0; i < count.length; i++)
{
count[i] = 0;
}
let n = s.length;
if (n == 1)
return true ;
for (let i = 0, j = n - 1; i < j; i++, j--)
{
count[s[i] - 'a' ]++;
count[s[j] - 'a' ]--;
}
for (let i = 0; i < MAX_CHAR; i++)
if (count[i] != 0)
return false ;
return true ;
}
let s = "abab" ;
if (checkCorrectOrNot(s))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity : O(n)
Auxiliary Space: O(1)
Last Updated :
14 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...