Skip to content

Commit

Permalink
Create 26. Unique Substrings in Wraparound String.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Dec 8, 2022
1 parent cbd9ca8 commit e848128
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# https://leetcode.com/problems/unique-substrings-in-wraparound-string/

from collections import defaultdict

class Solution:
def findSubstringInWraproundString(self, p: str) -> int:
dp = defaultdict(int)
streak = 0
for i in range(len(p)):
if (ord(p[i-1]) - 96) % 26 == (ord(p[i]) - 97):
streak += 1
else:
streak = 1
dp[p[i]] = max(dp[p[i]], streak)
# print(dp)
return sum(dp.values())



class Solution(object):
def findSubstringInWraproundString(self, p):
p, d, lo = '0'+p, collections.defaultdict(int), 0
for hi in range(1, len(p)):
if p[hi-1]+p[hi] not in 'abcdefghijklmnopqrstuvwxyza':
lo = hi
d[p[hi]] = max(d[p[hi]], hi+1-lo)
return sum(d.values())

0 comments on commit e848128

Please sign in to comment.