Posts Multi String Search (Trie)
Post
Cancel

Multi String Search (Trie)

PROBLEM DESCRIPTION

You are given a Big String and an array of small strings. You need to check whether each small string is present in the big string. Do not use any in-built method.

SOLUTION

We can make use of Trie to solve this problem. We loop through the bigstring and keep adding the subarrays [0,n-1] [1,n-1], [2,n-1] and so on. Then, all we need is to check whether the smallstring is present in the Trie as a prefix.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.util.*;

class Program {
  
  public static List<Boolean> multiStringSearch(String bigString, String[] smallStrings) {

        Trie trie = new Trie();
        for(int i=0; i<bigString.length(); i++){
            trie.insert(bigString.substring(i));
        }

        List<Boolean> list = new ArrayList<>();
        for(int i=0; i<smallStrings.length; i++){
            list.add(i, trie.startsWith(smallStrings[i]));
        }

        return list;
    
  }
}

class Trie {
    
    Node root;
    
    class Node{
        boolean isEnd;
        Map<Character, Node> hm;
        Node(Map<Character, Node> hm){
            this.hm = hm;
        }
    }
    
    public Trie() {
        root = new Node(new HashMap<>());
    }
    
    public void insert(String word) {
        
        int n = word.length();
        
        Node temp = root;
        
        for(int i=0; i<n; i++){
            
            Character c = Character.valueOf(word.charAt(i));
            
            if(temp.hm.containsKey(c)){
                temp = temp.hm.get(c);
            }else{
                temp.hm.put(c, new Node(new HashMap<>()));
                temp = temp.hm.get(c);
            }
            
            
        }
        
        temp.isEnd = true;
        
    }
    
    //Return true if complete match is found
    public boolean search(String word) {
        
        int n = word.length();
        
        Node temp = root;
        
        for(int i=0; i<n; i++){
            
            Character c = Character.valueOf(word.charAt(i));
            
            if(!temp.hm.containsKey(c)){
                return false;
            }else{
                temp = temp.hm.get(c);
            }
            
        }
        
        return temp.isEnd;

    }
    
    //Returns true is the input string is found (can be a prefix) 
    public boolean startsWith(String prefix) {
        int n = prefix.length();
        
        Node temp = root;
        
        for(int i=0; i<n; i++){
            
            Character c = Character.valueOf(prefix.charAt(i));
            
            if(!temp.hm.containsKey(c)){
                return false;
            }else{
                temp = temp.hm.get(c);
            }
            
        }
        
        return true;
    }
}
This post is licensed under CC BY 4.0 by the author.