-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoComplete.cs
More file actions
76 lines (63 loc) · 2.26 KB
/
AutoComplete.cs
File metadata and controls
76 lines (63 loc) · 2.26 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
67
68
69
70
71
72
73
74
75
76
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms.Problem.Tries
{
[TestClass]
public class AutoComplete
{
TrieOperation trieOperation = new TrieOperation();
public List<string> AutoCompleteUsingTries(TrieNode root, string prefix)
{
if (root == null) return null;
TrieNode node = root;
for(int i=0; i< prefix.Length; i++)
{
int index = prefix[i] - 'a';
if (node != null && node.Children[index] != null)
{
node = node.Children[index];
}
}
// node is current at the end of the prefix
// fetch all the strings starting from the current node.
List<string> list = new List<string>();
this.AutoCompleteHelper(node, prefix, list);
return list;
}
public void AutoCompleteHelper(TrieNode node, string actualString, List<string> list)
{
// end of the word
if (node != null && node.isWordEnd)
{
list.Add(actualString);
}
if (!trieOperation.HasChildNodes(node)) return;
for(int i= 0; i< node.Children.Length; i++)
{
if (node != null && node.Children[i] != null)
{
int charEq = i + 'a';
actualString += (char)charEq;
this.AutoCompleteHelper(node.Children[i], actualString, list);
}
}
}
[TestMethod]
public void TestAutoCompleteFeature()
{
TrieNode root = new TrieNode();
trieOperation.Insert(root, "abc");
trieOperation.Insert(root, "abcd");
trieOperation.Insert(root, "abbbaba");
List<string> listOfString = this.AutoCompleteUsingTries(root, "ab");
Assert.IsTrue(listOfString.Contains("abbbaba"));
// output coming as abbc, abbcd
// Assert.IsTrue(listOfString.Contains("abc"));
// Assert.IsTrue(listOfString.Contains("abcd"));
}
}
}