-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3093-LongestCommonSuffixQueries.go
More file actions
112 lines (99 loc) · 5.91 KB
/
3093-LongestCommonSuffixQueries.go
File metadata and controls
112 lines (99 loc) · 5.91 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
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
106
107
108
109
110
111
112
package main
// 3093. Longest Common Suffix Queries
// You are given two arrays of strings wordsContainer and wordsQuery.
// For each wordsQuery[i], you need to find a string from wordsContainer that has the longest common suffix with wordsQuery[i].
// If there are two or more strings in wordsContainer that share the longest common suffix, find the string that is the smallest in length.
// If there are two or more such strings that have the same smallest length, find the one that occurred earlier in wordsContainer.
// Return an array of integers ans, where ans[i] is the index of the string in wordsContainer that has the longest common suffix with wordsQuery[i].
// Example 1:
// Input: wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]
// Output: [1,1,1]
// Explanation:
// Let's look at each wordsQuery[i] separately:
// For wordsQuery[0] = "cd", strings from wordsContainer that share the longest common suffix "cd" are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
// For wordsQuery[1] = "bcd", strings from wordsContainer that share the longest common suffix "bcd" are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
// For wordsQuery[2] = "xyz", there is no string from wordsContainer that shares a common suffix. Hence the longest common suffix is "", that is shared with strings at index 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
// Example 2:
// Input: wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]
// Output: [2,0,2]
// Explanation:
// Let's look at each wordsQuery[i] separately:
// For wordsQuery[0] = "gh", strings from wordsContainer that share the longest common suffix "gh" are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.
// For wordsQuery[1] = "acbfgh", only the string at index 0 shares the longest common suffix "fgh". Hence it is the answer, even though the string at index 2 is shorter.
// For wordsQuery[2] = "acbfegh", strings from wordsContainer that share the longest common suffix "gh" are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.
// Constraints:
// 1 <= wordsContainer.length, wordsQuery.length <= 10^4
// 1 <= wordsContainer[i].length <= 5 * 10^3
// 1 <= wordsQuery[i].length <= 5 * 10^3
// wordsContainer[i] consists only of lowercase English letters.
// wordsQuery[i] consists only of lowercase English letters.
// Sum of wordsContainer[i].length is at most 5 * 10^5.
// Sum of wordsQuery[i].length is at most 5 * 10^5.
import "fmt"
type Trie struct {
ch []*Trie
index int // stores min string index till current ch
}
func NewTrie(index int) *Trie {
return &Trie{ ch: make([]*Trie, 26), index: index }
}
func (t *Trie) Insert(str string, index int, ws []string) {
temp := t
for i := len(str) - 1; i >= 0; i-- {
k := int(str[i] - 'a')
if temp.ch[k] == nil {
temp.ch[k] = NewTrie(index)
}
curr := temp.ch[k].index
if len(ws[curr]) > len(str) {
temp.ch[k].index = index
}
temp = temp.ch[k]
}
}
func (t *Trie) Find(str string) int {
temp := t
for i := len(str) - 1; i >= 0; i-- {
k := int(str[i] - 'a')
if temp.ch[k] == nil { break }
temp = temp.ch[k]
}
return temp.index
}
func stringIndices(wordsContainer []string, wordsQuery []string) []int {
t := NewTrie(-1)
res, mn := make([]int, len(wordsQuery)), 0
for i, str := range wordsContainer {
t.Insert(str, i, wordsContainer)
if len(wordsContainer[mn]) > len(str) {
mn = i
}
}
for i, str := range wordsQuery {
res[i] = t.Find(str)
if res[i] == -1 {
res[i] = mn
}
}
return res
}
func main() {
// Example 1:
// Input: wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]
// Output: [1,1,1]
// Explanation:
// Let's look at each wordsQuery[i] separately:
// For wordsQuery[0] = "cd", strings from wordsContainer that share the longest common suffix "cd" are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
// For wordsQuery[1] = "bcd", strings from wordsContainer that share the longest common suffix "bcd" are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
// For wordsQuery[2] = "xyz", there is no string from wordsContainer that shares a common suffix. Hence the longest common suffix is "", that is shared with strings at index 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
fmt.Println(stringIndices([]string{"abcd","bcd","xbcd"}, []string{"cd","bcd","xyz"})) // [1,1,1]
// Example 2:
// Input: wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]
// Output: [2,0,2]
// Explanation:
// Let's look at each wordsQuery[i] separately:
// For wordsQuery[0] = "gh", strings from wordsContainer that share the longest common suffix "gh" are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.
// For wordsQuery[1] = "acbfgh", only the string at index 0 shares the longest common suffix "fgh". Hence it is the answer, even though the string at index 2 is shorter.
// For wordsQuery[2] = "acbfegh", strings from wordsContainer that share the longest common suffix "gh" are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.
fmt.Println(stringIndices([]string{"abcdefgh","poiuygh","ghghgh"}, []string{"gh","acbfgh","acbfegh"})) // [2,0,2]
}