567. Permutation in String (Leetcode Problem)

preview_player
Показать описание
#leetcode #geeksforgeeks #adobe #amazon #microsoft #dailychallenge #technicalinterview #problemoftheday

Solution Code: Available in comment section

Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.

In other words, return true if one of s1's permutations is the substring of s2.
Рекомендации по теме
Комментарии
Автор

Solution Code (Java):
class Solution {
public boolean checkInclusion(String s1, String s2) {
int a[]=new int[26];
for(int i=0;i<s1.length();i++)
a[s1.charAt(i)-'a']++;
int b[]=new int[26];
for(int i=0;i<s2.length();i++)
{
if(i<s1.length())
{
b[s2.charAt(i)-'a']++;
if(i<s1.length()-1) continue;
}
else
{
b[s2.charAt(i)-'a']++;

}
if(match(a, b))
return true;
}
return false;
}
static boolean match(int a[], int b[])
{
for(int i=0;i<26;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
}

viralSongsBollywood
Автор

bhai ye last vale ke lie nhi chl rha uske lie ik or condition add krni pd rhi ha
kyuki
jb vo element add krega to loop break ho jayga or vo vala window check nhi hone ka


bool match(int arr1[], int arr2[])
{
for(int i=0; i<26; i++)
{
if(arr1[i] != arr2[i])
{
return false;
}
}
return true;
}
bool checkInclusion(string s1, string s2) {
int arr1[26] = {0};
int arr2[26] = {0};
for(int i=0; i<s1.length(); i++)
{
arr1[s1[i]-'a']++;
}
for(int i=0; i<s2.length(); i++)
{
if( i < s1.size() )
{
arr2[s2[i]-'a']++;
continue;
}
else{
if(match(arr1, arr2))
return true;

arr2[s2[i]-'a']++;
}
}
if(match(arr1, arr2))
{
return true;
}
return false;
}

ShivamTyagi
join shbcf.ru