Skip to content

Commit 4676dbd

Browse files
authored
Create get-equal-substrings-within-budget.py
1 parent 9cadb2c commit 4676dbd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def equalSubstring(self, s, t, maxCost):
6+
"""
7+
:type s: str
8+
:type t: str
9+
:type maxCost: int
10+
:rtype: int
11+
"""
12+
left = 0
13+
for right in xrange(len(s)):
14+
maxCost -= abs(ord(s[right])-ord(t[right]))
15+
if maxCost < 0:
16+
maxCost += abs(ord(s[left])-ord(t[left]))
17+
left += 1
18+
return (right+1)-left

0 commit comments

Comments
 (0)