-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC567.java
More file actions
62 lines (47 loc) · 1.3 KB
/
LC567.java
File metadata and controls
62 lines (47 loc) · 1.3 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* LC567
*/
import java.util.*;
public class LC567 {
public static boolean checkInclusion(String s1, String s2) {
int n = s1.length();
int m = s2.length();
if (m < n) {
return false;
}
int map1[] = new int[26];
for (int i = 0; i < n; i++) {
map1[s1.charAt(i) - 'a']++;
}
for (int i = 0; i <= m - n; i++) {
int map2[] = new int[26];
for (int j = 0; j < n; j++) {
map2[s2.charAt(i + j) - 'a']++;
}
if (isMatched(map1, map2)) {
return true;
}
}
return false;
}
private static boolean isMatched(int map1[], int map2[]) {
for (int i = 0; i < 26; i++) {
if (map1[i] != map2[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter The String S1 : ");
String s1 = sc.nextLine();
System.out.println();
System.out.print("Enter The String S2 : ");
String s2 = sc.nextLine();
System.out.println();
boolean ans = checkInclusion(s1, s2);
System.out.println(ans);
sc.close();
}
}