Find whether X exists in Y after jumbling X
Given two strings X and Y containing lower-case alphabets, the task is to check whether any permutation of string X exists in Y as its substring.
Examples:
Input: X = “skege”, Y = “geeksforgeeks”
Output: Yes
“geeks” is a permutation of X which
appears as a substring in Y.
Input: X = “aabb”, Y = “bbbbbbb”
Output: No
Approach: This problem can be solved using the two pointer technique.
- Compute the frequency count of every character of string X and store it in an array say cnt_X[].
- Now for substring Y[i…(i+X.length()-1)], the same frequency array can be generated say cnt[].
- Using the array from step 2, the frequency count for the next window can be calculated in O(1) time by decrementing cnt[Y[i] – ‘a’] by 1 and incrementing cnt[Y[i + X.length()] – ‘a’] by 1.
- Compare cnt[] and cnt_X[] for every window. If both of them are equal then a match has been found.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 26;
bool areEqual( int * a, int * b)
{
for ( int i = 0; i < MAX; i++)
if (a[i] != b[i])
return false ;
return true ;
}
bool xExistsInY(string x, string y)
{
if (x.size() > y.size())
return false ;
int cnt_x[MAX] = { 0 };
int cnt[MAX] = { 0 };
for ( int i = 0; i < x.size(); i++)
cnt_x[x[i] - 'a' ]++;
for ( int i = 0; i < x.size(); i++)
cnt[y[i] - 'a' ]++;
if (areEqual(cnt_x, cnt))
return true ;
for ( int i = 1; i < y.size() - x.size() + 1; i++) {
cnt[y[i - 1] - 'a' ]--;
cnt[y[i + x.size() - 1] - 'a' ]++;
if (areEqual(cnt, cnt_x))
return true ;
}
return false ;
}
int main()
{
string x = "skege" ;
string y = "geeksforgeeks" ;
if (xExistsInY(x, y))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static int MAX = 26 ;
static boolean areEqual( int []a, int []b)
{
for ( int i = 0 ; i < MAX; i++)
if (a[i] != b[i])
return false ;
return true ;
}
static boolean xExistsInY(String x, String y)
{
if (x.length() > y.length())
return false ;
int []cnt_x = new int [MAX];
int []cnt = new int [MAX];
for ( int i = 0 ; i < x.length(); i++)
cnt_x[x.charAt(i) - 'a' ]++;
for ( int i = 0 ; i < x.length(); i++)
cnt[y.charAt(i) - 'a' ]++;
if (areEqual(cnt_x, cnt))
return true ;
for ( int i = 1 ; i < y.length() -
x.length() + 1 ; i++)
{
cnt[y.charAt(i - 1 ) - 'a' ]--;
cnt[y.charAt(i + x.length() - 1 ) - 'a' ]++;
if (areEqual(cnt, cnt_x))
return true ;
}
return false ;
}
public static void main(String[] args)
{
String x = "skege" ;
String y = "geeksforgeeks" ;
if (xExistsInY(x, y))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
MAX = 26
def areEqual(a, b):
for i in range ( MAX ):
if (a[i] ! = b[i]):
return False
return True
def xExistsInY(x,y):
if ( len (x) > len (y)):
return False
cnt_x = [ 0 ] * MAX
cnt = [ 0 ] * MAX
for i in range ( len (x)):
cnt_x[ ord (x[i]) - ord ( 'a' )] + = 1 ;
for i in range ( len (x)):
cnt[ ord (y[i]) - ord ( 'a' )] + = 1
if (areEqual(cnt_x, cnt)):
return True
for i in range ( 1 , len (y) - len (x) + 1 ):
cnt[ ord (y[i - 1 ]) - ord ( 'a' )] - = 1
cnt[ ord (y[i + len (x) - 1 ]) - ord ( 'a' )] + = 1
if (areEqual(cnt, cnt_x)):
return True
return False
if __name__ = = '__main__' :
x = "skege"
y = "geeksforgeeks"
if (xExistsInY(x, y)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static int MAX = 26;
static bool areEqual( int []a,
int []b)
{
for ( int i = 0; i < MAX; i++)
if (a[i] != b[i])
return false ;
return true ;
}
static bool xExistsInY(String x,
String y)
{
if (x.Length > y.Length)
return false ;
int []cnt_x = new int [MAX];
int []cnt = new int [MAX];
for ( int i = 0; i < x.Length; i++)
cnt_x[x[i] - 'a' ]++;
for ( int i = 0; i < x.Length; i++)
cnt[y[i] - 'a' ]++;
if (areEqual(cnt_x, cnt))
return true ;
for ( int i = 1; i < y.Length -
x.Length + 1; i++)
{
cnt[y[i - 1] - 'a' ]--;
cnt[y[i + x.Length - 1] - 'a' ]++;
if (areEqual(cnt, cnt_x))
return true ;
}
return false ;
}
public static void Main(String[] args)
{
String x = "skege" ;
String y = "geeksforgeeks" ;
if (xExistsInY(x, y))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
var MAX = 26;
function areEqual(a, b)
{
for ( var i = 0; i < MAX; i++)
if (a[i] != b[i])
return false ;
return true ;
}
function xExistsInY(x, y)
{
if (x.length > y.length)
return false ;
var cnt_x = Array(MAX).fill(0);
var cnt = Array(MAX).fill(0);
for ( var i = 0; i < x.length; i++)
cnt_x[x[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
for ( var i = 0; i < x.length; i++)
cnt[y[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
if (areEqual(cnt_x, cnt))
return true ;
for ( var i = 1; i < y.length - x.length + 1; i++) {
cnt[y[i - 1].charCodeAt(0) - 'a' .charCodeAt(0)]--;
cnt[y[i + x.length - 1].charCodeAt(0) - 'a' .charCodeAt(0)]++;
if (areEqual(cnt, cnt_x))
return true ;
}
return false ;
}
var x = "skege" ;
var y = "geeksforgeeks" ;
if (xExistsInY(x, y))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(xLen + yLen) where xLen and yLen are the lengths of the strings X and Y respectively.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
28 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...