Skip to content

Commit 30db9fe

Browse files
authored
Create Count Prefix and Suffix Pairs I.py
1 parent 933bb23 commit 30db9fe

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Count Prefix and Suffix Pairs I.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
You are given a 0-indexed string array words.
3+
4+
Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2:
5+
6+
isPrefixAndSuffix(str1, str2) returns true if str1 is both a
7+
prefix
8+
and a
9+
suffix
10+
of str2, and false otherwise.
11+
For example, isPrefixAndSuffix("aba", "ababa") is true because "aba" is a prefix of "ababa" and also a suffix, but isPrefixAndSuffix("abc", "abcd") is false.
12+
13+
Return an integer denoting the number of index pairs (i, j) such that i < j, and isPrefixAndSuffix(words[i], words[j]) is true.
14+
15+
'''
16+
17+
class Solution:
18+
def countPrefixSuffixPairs(self, words: List[str]) -> int:
19+
def helper(w1,w2):
20+
return w2.startswith(w1) and w2.endswith(w1)
21+
22+
n =len(words)
23+
res = 0
24+
for i in range(n):
25+
for j in range(n):
26+
if i!=j and i<j and helper(words[i],words[j]):
27+
res+=1
28+
return res

0 commit comments

Comments
 (0)