-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount-prefix-and-suffix-pairs-i.py
More file actions
88 lines (69 loc) · 2.91 KB
/
Copy pathcount-prefix-and-suffix-pairs-i.py
File metadata and controls
88 lines (69 loc) · 2.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
class Node:
def __init__(self):
self.links = [None] * 26
# Check if the character is present in the current node
def _contains(self, c: str) -> bool:
return self.links[ord(c) - ord("a")] is not None
# Insert a new node for the character
def _put(self, c: str, node: "Node") -> None:
self.links[ord(c) - ord("a")] = node
# Get the next node for the character
def _next(self, c: str) -> "Node":
return self.links[ord(c) - ord("a")]
class Trie:
def __init__(self):
self.root = Node()
# Insert a word into the Trie
def insert(self, word: str) -> None:
node = self.root
for c in word:
if not node._contains(c):
node._put(c, Node())
node = node._next(c)
# Check if the Trie contains a given prefix
def starts_with(self, prefix: str) -> bool:
node = self.root
for c in prefix:
if not node._contains(c):
return False
node = node._next(c)
return True
class Solution:
def countPrefixSuffixPairs(self, words: List[str]) -> int:
"""
You are given a 0-indexed string array words.
Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2:
isPrefixAndSuffix(str1, str2) returns true if str1 is both a
prefix
and a
suffix
of str2, and false otherwise.
For example, isPrefixAndSuffix("aba", "ababa") is true because "aba" is a prefix of "ababa" and also a suffix, but isPrefixAndSuffix("abc", "abcd") is false.
Return an integer denoting the number of index pairs (i, j) such that i < j, and isPrefixAndSuffix(words[i], words[j]) is true.
"""
n = len(words)
count = 0
# Step 1: Iterate over each word
for i in range(n):
prefix_trie = Trie()
suffix_trie = Trie()
# Step 2: Insert the current word into the prefix Trie
prefix_trie.insert(words[i])
# Step 3: Reverse the word and insert it into the suffix Trie
rev_word = words[i][::-1]
suffix_trie.insert(rev_word)
# Step 4: Iterate over all previous words
for j in range(i):
# Step 5: Skip words[j] if it is longer than words[i]
if len(words[j]) > len(words[i]):
continue
# Step 6: Extract the prefix and reversed prefix of words[j]
prefix_word = words[j]
rev_prefix_word = prefix_word[::-1]
# Step 7: Check if words[j] is both a prefix and suffix of words[i]
if prefix_trie.starts_with(
prefix_word
) and suffix_trie.starts_with(rev_prefix_word):
count += 1
# Step 8: Return the total count of valid pairs
return count