Skip to content

Commit 235c51e

Browse files
authored
Create count-pairs-of-equal-substrings-with-minimum-difference.py
1 parent 9eb7570 commit 235c51e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def countQuadruples(self, firstString, secondString):
6+
"""
7+
:type firstString: str
8+
:type secondString: str
9+
:rtype: int
10+
"""
11+
lookup1 = [-1]*26
12+
for i in reversed(xrange(len(firstString))):
13+
lookup1[ord(firstString[i])-ord('a')] = i
14+
lookup2 = [-1]*26
15+
for i in xrange(len(secondString)):
16+
lookup2[ord(secondString[i])-ord('a')] = i
17+
result, diff = 0, float("inf")
18+
for i in xrange(26):
19+
if lookup1[i] == -1 or lookup2[i] == -1:
20+
continue
21+
if lookup1[i]-lookup2[i] < diff:
22+
diff = lookup1[i]-lookup2[i]
23+
result = 0
24+
result += int(lookup1[i]-lookup2[i] == diff)
25+
return result

0 commit comments

Comments
 (0)