Problem
A string s
is nice if, for every letter of the alphabet that s
contains, it appears both in uppercase and lowercase. For example, "abABB"
is nice because 'A'
and 'a'
appear, and 'B'
and 'b'
appear. However, "abA"
is not because 'b'
appears, but 'B'
does not.
Given a string s
, return the longest substring of s
that is nice. If there are multiple, return the substring of the earliest occurrence. If there are none, return an empty string.
Example 1:
1 | Input: s = "YazaAay" |
Example 2:
1 | Input: s = "Bb" |
Example 3:
1 | Input: s = "c" |
Constraints:
1 <= s.length <= 100
s
consists of uppercase and lowercase English letters.
Analysis
题目给出一个字符串s
,定义nice substring是substring中每个字符的大小写都出现过,要求返回最长的nice substring。字符串题目中substring的题目一般有几种解法,一种是滑动窗口,另一种是分治。这里滑动窗口其实并不是很适合,原因是我们很难定义收窄窗口的条件,我们要维护窗口里面有什么字符,还要知道出现的顺序。所以这里我用的是分治,首先我们要记录字符串中出现过的所有字符,这样我们才能判断出某个字符的大小写是否都出现过,只有满足要求的字符我们才会考虑,如果不满足,说明substring肯定不包含这个字符。
先看分治的解法,对于不满足要求的字符,递归调用其左、右子串,并且返回长度较大的那个,最后就是结果。
Solution
无。
Code
1 | class Solution { |
Summary
这道题目告诉我们数组中subarray不一定都是用sliding window,因为这里window收窄条件并不容易写出来,所以更好的做法应该是采用分治,分治的好处在于它省去了从左到右一直维护window信息的麻烦,只需要focus在当前的子串。这道题目的分享到这里,感谢你的支持!