forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 26. Unique Substrings in Wraparound String.py
- Loading branch information
1 parent
cbd9ca8
commit e848128
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
02_Dynamic-Programming/12. DP Using 1D Array/26. Unique Substrings in Wraparound String.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |