-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20250108.py
More file actions
129 lines (99 loc) · 3.99 KB
/
20250108.py
File metadata and controls
129 lines (99 loc) · 3.99 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'''
https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/description
Count Prefix and Suffix Pairs I
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.
Example 1:
Input: words = ["a","aba","ababa","aa"]
Output: 4
Explanation: In this example, the counted index pairs are:
i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
Therefore, the answer is 4.
Example 2:
Input: words = ["pa","papa","ma","mama"]
Output: 2
Explanation: In this example, the counted index pairs are:
i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
Therefore, the answer is 2.
Example 3:
Input: words = ["abab","ab"]
Output: 0
Explanation: In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
Therefore, the answer is 0.
Constraints:
1 <= words.length <= 50
1 <= words[i].length <= 10
words[i] consists only of lowercase English letters.
'''
from typing import List
class Solution:
def countPrefixSuffixPairs(self, words: List[str]) -> int:
start = 0
end = len(words)-1
count = 0
while start < end:
current = words[start]
for i in range(start, len(words)):
if i == start:
continue
if isPrefixAndSuffix(current, words[i]) == True:
count += 1
start += 1
return count
def isPrefixAndSuffix(str1: str, str2: str) -> bool:
str1_len = len(str1)
str2_prefix = str2[:str1_len]
str2_suffix = str2[-str1_len:]
if len(str1) > len(str2):
return False
if str1 == str2_prefix and str1 == str2_suffix:
return True
return False
solution = Solution()
print(solution.countPrefixSuffixPairs(["a","aba","ababa","aa"])) # -> 4
print(solution.countPrefixSuffixPairs(["pa","papa","ma","mama"])) # -> 2
print(solution.countPrefixSuffixPairs(["abab","ab"])) # -> 0
print(solution.countPrefixSuffixPairs(["bb","bb"])) # -> 1
print(solution.countPrefixSuffixPairs(["b","b","bb"])) # -> 3
'''
Pseudocode:
- Define countPrefixSuffixPairs
- Initialize start at zero
- Declare end as length of words minus one
- Initialize count at zero
- While start is less than end:
- Loop through the words list starting at start and ending ad length of words
- If i is equal to start:
- Continue (don't double count)
- If the call of isPrefixAndSuffix is equal to True:
- Increase count by one
- Increase start by one
- Return count
- Define isPrefixAndSuffix
- Declare str1_len as the length of str1
- Declare str2_prefix as str2 sliced from beginning to str1_len
- Declare str2_suffix as str2 sliced beginning at negative str1_len until the end
- Note: This returns the last str1_len values of the list
- If length of str1 is greater then length of str2
- return False
- If str1 is equal to str2_prefix and str1 is equal to str2_suffix:
- return True
- Return False
- Note: This is not my finest code, but it works (due to the forgiving constraints). O(n^3) time complexity - worth a refactor in the future.
- Ways to improve:
- Avoid nested loops:
- A hashmap or trie for storing prefixes and suffixes would help reduce redundant comparisions
- Precompute prefixes and suffixes:
- Precompute all valid prefixes and suffixes for each word and store them in a data structure that supports fast lookups.
'''