14
14
import java .util .ArrayList ;
15
15
import java .util .HashMap ;
16
16
import java .util .LinkedList ;
17
+ import java .util .List ;
17
18
import java .util .Map ;
18
19
import java .util .Queue ;
19
20
@@ -24,7 +25,7 @@ private AhoCorasick() {
24
25
// Trie Node Class
25
26
private static class Node {
26
27
// Represents a character in the trie
27
- private HashMap <Character , Node > child = new HashMap <>(); // Child nodes of the current node
28
+ private final Map <Character , Node > child = new HashMap <>(); // Child nodes of the current node
28
29
private Node suffixLink ; // Suffix link to another node in the trie
29
30
private Node outputLink ; // Output link to another node in the trie
30
31
private int patternInd ; // Index of the pattern that ends at this node
@@ -35,7 +36,7 @@ private static class Node {
35
36
this .patternInd = -1 ;
36
37
}
37
38
38
- public HashMap <Character , Node > getChild () {
39
+ public Map <Character , Node > getChild () {
39
40
return child ;
40
41
}
41
42
@@ -148,16 +149,16 @@ private void buildSuffixAndOutputLinks() {
148
149
}
149
150
}
150
151
151
- private ArrayList < ArrayList <Integer >> initializePositionByStringIndexValue () {
152
- ArrayList < ArrayList <Integer >> positionByStringIndexValue = new ArrayList <>(patterns .length ); // Stores positions where patterns are found in the text
152
+ private List < List <Integer >> initializePositionByStringIndexValue () {
153
+ List < List <Integer >> positionByStringIndexValue = new ArrayList <>(patterns .length ); // Stores positions where patterns are found in the text
153
154
for (int i = 0 ; i < patterns .length ; i ++) {
154
- positionByStringIndexValue .add (new ArrayList <Integer >());
155
+ positionByStringIndexValue .add (new ArrayList <>());
155
156
}
156
157
return positionByStringIndexValue ;
157
158
}
158
159
159
160
// Searches for patterns in the input text and records their positions
160
- public ArrayList < ArrayList <Integer >> searchIn (final String text ) {
161
+ public List < List <Integer >> searchIn (final String text ) {
161
162
var positionByStringIndexValue = initializePositionByStringIndexValue (); // Initialize a list to store positions of the current pattern
162
163
Node parent = root ; // Start searching from the root node
163
164
@@ -187,7 +188,7 @@ public ArrayList<ArrayList<Integer>> searchIn(final String text) {
187
188
188
189
// by default positionByStringIndexValue contains end-points. This function converts those
189
190
// endpoints to start points
190
- private void setUpStartPoints (ArrayList < ArrayList <Integer >> positionByStringIndexValue ) {
191
+ private void setUpStartPoints (List < List <Integer >> positionByStringIndexValue ) {
191
192
for (int i = 0 ; i < patterns .length ; i ++) {
192
193
for (int j = 0 ; j < positionByStringIndexValue .get (i ).size (); j ++) {
193
194
int endpoint = positionByStringIndexValue .get (i ).get (j );
@@ -198,20 +199,15 @@ private void setUpStartPoints(ArrayList<ArrayList<Integer>> positionByStringInde
198
199
}
199
200
200
201
// Class to handle pattern position recording
201
- private static class PatternPositionRecorder {
202
- private ArrayList <ArrayList <Integer >> positionByStringIndexValue ;
203
-
202
+ private record PatternPositionRecorder (List <List <Integer >> positionByStringIndexValue ) {
204
203
// Constructor to initialize the recorder with the position list
205
- PatternPositionRecorder (final ArrayList <ArrayList <Integer >> positionByStringIndexValue ) {
206
- this .positionByStringIndexValue = positionByStringIndexValue ;
207
- }
208
204
209
205
/**
210
206
* Records positions for a pattern when it's found in the input text and follows
211
207
* output links to record positions of other patterns.
212
208
*
213
- * @param parent The current node representing a character in the pattern trie.
214
- * @param currentPosition The current position in the input text.
209
+ * @param parent The current node representing a character in the pattern trie.
210
+ * @param currentPosition The current position in the input text.
215
211
*/
216
212
public void recordPatternPositions (final Node parent , final int currentPosition ) {
217
213
// Check if the current node represents the end of a pattern
@@ -229,19 +225,20 @@ public void recordPatternPositions(final Node parent, final int currentPosition)
229
225
}
230
226
}
231
227
}
228
+
232
229
// method to search for patterns in text
233
- public static Map <String , ArrayList <Integer >> search (final String text , final String [] patterns ) {
230
+ public static Map <String , List <Integer >> search (final String text , final String [] patterns ) {
234
231
final var trie = new Trie (patterns );
235
232
final var positionByStringIndexValue = trie .searchIn (text );
236
233
return convert (positionByStringIndexValue , patterns );
237
234
}
238
235
239
236
// method for converting results to a map
240
- private static Map <String , ArrayList <Integer >> convert (final ArrayList < ArrayList <Integer >> positionByStringIndexValue , final String [] patterns ) {
241
- Map <String , ArrayList <Integer >> positionByString = new HashMap <>();
237
+ private static Map <String , List <Integer >> convert (final List < List <Integer >> positionByStringIndexValue , final String [] patterns ) {
238
+ Map <String , List <Integer >> positionByString = new HashMap <>();
242
239
for (int i = 0 ; i < patterns .length ; i ++) {
243
240
String pattern = patterns [i ];
244
- ArrayList <Integer > positions = positionByStringIndexValue .get (i );
241
+ List <Integer > positions = positionByStringIndexValue .get (i );
245
242
positionByString .put (pattern , new ArrayList <>(positions ));
246
243
}
247
244
return positionByString ;
0 commit comments