-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay36.java
More file actions
66 lines (51 loc) · 2.16 KB
/
Day36.java
File metadata and controls
66 lines (51 loc) · 2.16 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
63
64
65
66
import java.util.ArrayList;
import java.util.List;
class Day36 {
public static List<Integer> findOccurrences(String text, String pattern) {
List<Integer> occurrences = new ArrayList<>();
int n = text.length();
int m = pattern.length();
if (m > n) {
return occurrences;
}
long patternHash = calculateHash(pattern);
long textHash = calculateHash(text.substring(0, m));
if (patternHash == textHash && pattern.equals(text.substring(0, m))) {
occurrences.add(1); // Pattern found at the beginning
}
long power = 1;
for (int i = 0; i < m - 1; i++) {
power = (power * 256) % 101; // 101 is a prime number
}
for (int i = 1; i <= n - m; i++) {
textHash = (textHash + 101 - (text.charAt(i - 1) * power) % 101) % 101; // Remove the leftmost character
textHash = (textHash * 256 + text.charAt(i + m - 1)) % 101; // Add the next character
if (patternHash == textHash && pattern.equals(text.substring(i, i + m))) {
occurrences.add(i + 1); // Pattern found at position i+1
}
}
return occurrences;
}
private static long calculateHash(String s) {
long hash = 0;
int prime = 101; // A prime number
for (int i = 0; i < s.length(); i++) {
hash = (hash * 256 + s.charAt(i)) % prime;
}
return hash;
}
public static void main(String[] args) {
String text1 = "cxyzghxyzvjkxyz";
String pattern1 = "xyz";
List<Integer> occurrences1 = findOccurrences(text1, pattern1);
System.out.println("Sample Output 1: " + occurrences1); // Output: [2, 7, 13]
String text2 = "ababacabab";
String pattern2 = "aba";
List<Integer> occurrences2 = findOccurrences(text2, pattern2);
System.out.println("Sample Output 2: " + occurrences2); // Output: [1, 3, 7]
String text3 = "abcd";
String pattern3 = "xy";
List<Integer> occurrences3 = findOccurrences(text3, pattern3);
System.out.println("Sample Output 3: " + occurrences3); // Output: [0]
}
}