-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationOfPatternExist.java
More file actions
33 lines (30 loc) · 939 Bytes
/
PermutationOfPatternExist.java
File metadata and controls
33 lines (30 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package Strings;
import java.util.Arrays;
/**
* @author Vishal Singh
*/
public class PermutationOfPatternExist {
static boolean checkPattern(String string,String pattern){
int n = string.length();
int m = pattern.length();
int[] countPattern = new int[256];
int[] countString = new int[256];
for (int i = 0; i < pattern.length(); i++) {
countPattern[pattern.charAt(i)]++;
countString[string.charAt(i)]++;
}
for (int i = m; i < n; i++) {
if (Arrays.equals(countPattern,countString)){
return true;
}
countString[string.charAt(i)]++;
countString[string.charAt(i-m)]--;
}
return false;
}
public static void main(String[] args) {
String s1 = "malayalam";
String pattern = "yamal"; //malay
System.out.println(checkPattern(s1,pattern));
}
}