最长的美好子字符串
难度:
标签:
题目描述
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:
Input: s = "YazaAay" Output: "aAa" Explanation: "aAa" is a nice string because 'A/a' is the only letter of the alphabet in s, and both 'A' and 'a' appear. "aAa" is the longest nice substring.
Example 2:
Input: s = "Bb" Output: "Bb" Explanation: "Bb" is a nice string because both 'B' and 'b' appear. The whole string is a substring.
Example 3:
Input: s = "c" Output: "" Explanation: There are no nice substrings.
Constraints:
1 <= s.length <= 100
s
consists of uppercase and lowercase English letters.
代码结果
运行时间: 26 ms, 内存: 16.0 MB
/*
* 思路:
* 1. 使用Java Stream对字符串进行处理。
* 2. 生成所有可能的子字符串。
* 3. 过滤出美好子字符串。
* 4. 找到最长的美好子字符串,如果有多个,返回最早出现的。
*/
import java.util.stream.IntStream;
public class Solution {
public String longestNiceSubstring(String s) {
return IntStream.range(0, s.length())
.boxed()
.flatMap(i -> IntStream.range(i + 1, s.length() + 1)
.mapToObj(j -> s.substring(i, j)))
.filter(this::isNice)
.max((a, b) -> a.length() == b.length() ? s.indexOf(a) - s.indexOf(b) : a.length() - b.length())
.orElse("");
}
private boolean isNice(String s) {
return s.chars().allMatch(c -> Character.isLowerCase(c) ? s.indexOf(Character.toUpperCase(c)) >= 0 : s.indexOf(Character.toLowerCase(c)) >= 0);
}
}
解释
方法:
这个题解采用了递归的方式来找出最长的美好子字符串。首先,它检查整个字符串是否为美好字符串,即每个在字符串中出现的字符的大写和小写形式都必须同时存在。如果整个字符串是美好的,直接返回整个字符串。如果不是,它找到第一个不满足美好字符串条件的字符,然后将字符串分成左右两部分,在这两部分中递归地寻找最长的美好子字符串。最后,比较左右两部分得到的最长美好子字符串的长度,返回较长的那个。
时间复杂度:
O(n^2)
空间复杂度:
O(n)
代码细节讲解
🦆
为什么在检查是否为美好字符串时,仅通过字符的大写或小写形式在字符串中的存在来判断?是否有更高效的方式来验证字符的大小写形式同时存在?
▷🦆
在递归过程中,如果中间的字符不满足美好字符串的条件,为什么选择忽略这个字符而直接对左右两部分进行递归?是否有可能忽略了包含该字符的更长的美好子字符串?
▷🦆
在递归返回最长美好子字符串的过程中,比较左右字符串长度是否足够判断最长子字符串,还是也需要考虑它们的起始位置以确保返回最早出现的一个?
▷