Python: 1624. Largest Substring Between Two Equal Characters
1624. Largest Substring Between Two Equal Characters
Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.
A substring is a contiguous sequence of characters within a string.
同樣辦法,維持一個list,長度26,每個元素儲存的是第一個index of 那個字母
走過一次string,如果發現過去有某字母已經出現,則更新最大長度,否則initiate
class Solution: def maxLengthBetweenEqualCharacters(self, s: str) -> int: if len(set(s))==len(s): return -1 # index list d=[-1]*26 ans=0 for i,x in enumerate(s): if d[ord(x)-97]>-1: ans=max(ans,i-d[ord(x)-97]-1) else: d[ord(x)-97]=i return ans
留言
張貼留言