1763. Longest Nice Substring


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.


1 <= s.length <= 100
s consists of uppercase and lowercase English letters.


使用windows的方式, 通过i,j来进行移动, 将截取出来的字符串判断是否是nice, 如果是nice就进行比对即可.


class Solution {
    func longestNiceSubstring(_ s: String) -> String {
        var result = ""
        for i in s.indices {
            for j in s.indices {
                if (i < j) {
                    var tmpString = String(s[i...j])
                    if (isNiceString(tmpString)) {
                        result = result.count >= tmpString.count ? result : tmpString
        return result
    func isNiceString(_ str: String) -> Bool {
        return Double(Set(str.lowercased()).count) == (Double(Set(str).count) / 2.0)