package leetcode;

/**
 * https://leetcode.com/problems/longest-nice-substring/
 */
public class Problem1763 {
    public String longestNiceSubstring(String s) {
        String answer = "";
        for (int i = 0; i < s.length(); i++) {
            for (int j = i + 1; j < s.length(); j++) {
                String sub = s.substring(i, j + 1);
                if (isNice(sub)) {
                    if (sub.length() > answer.length()) {
                        answer = sub;
                    }
                }
            }
        }
        return answer;
    }

    private static boolean isNice(String s) {
        boolean[] lower = new boolean[26];
        boolean[] upper = new boolean[26];
        for (char c : s.toCharArray()) {
            if (Character.isUpperCase(c)) {
                upper[c - 'A'] = true;
            } else {
                lower[c - 'a'] = true;
            }
        }
        for (char c : s.toCharArray()) {
            if ((Character.isUpperCase(c) && !lower[c - 'A']) ||
                (Character.isLowerCase(c) && !upper[c - 'a'])) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Problem1763 prob = new Problem1763();
        System.out.println(prob.longestNiceSubstring("YazaAay")); // "aAa"
        System.out.println(prob.longestNiceSubstring("Bb")); // "Bb"
        System.out.println(prob.longestNiceSubstring("c")); // ""
        System.out.println(prob.longestNiceSubstring("dDzeE")); // "dD"
        System.out.println(prob.longestNiceSubstring("aBbA")); // "aBbA"
        System.out.println(prob.longestNiceSubstring("aBbAa")); // "aBbAa"
    }
}

@@ -167,440 +167,360 @@
     
-String answer = %22%22
+return getLongestNiceSubstring(s).string
 ;%0A    
+%7D%0A%0A
     
-for (int i = 0; i %3C s.length(); i++) %7B%0A            for (int j = i + 1; j %3C s.length(); j++) %7B%0A                String sub = s.substring(i, j + 1);%0A                if (isNice(sub)) %7B%0A                    if (sub.length() %3E answer.length()) %7B%0A                        answer = sub;%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        return answer;%0A    %7D%0A%0A    private static boolean isNice
+private static class NiceString %7B%0A        private final String string;%0A        private final boolean nice;%0A%0A        public NiceString(String string, boolean nice) %7B%0A            this.string = string;%0A            this.nice = nice;%0A        %7D%0A    %7D%0A%0A    private static NiceString getLongestNiceSubstring
 (Str
@@ -839,35 +839,75 @@
 or (
+int i = 0; i %3C s.length(); i++) %7B%0A            
 char c 
-: s.toC
+= s.c
 harA
-rray()) %7B
+t(i);
 %0A   
@@ -1053,20 +1053,67 @@
     
-return false
+NiceString ns1 = getLongestNiceSubstring(s.substring(0, i))
 ;%0A  
@@ -1126,568 +1126,642 @@
     
-%7D%0A
     
-    %7D%0A        return true;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1763 prob = new Problem1763();%0A        System.out.println(prob.longestNiceSubstring(%22YazaAay%22)); // %22aAa%22%0A        System.out.println(prob.longestNiceSubstring(%22Bb%22)); // %22Bb%22%0A        System.out.println(prob.longestNiceSubstring(%22c%22)); // %22%22%0A        System.out.println(prob.longestNiceSubstring(%22dDzeE%22)); // %22dD%22%0A        System.out.println(prob.longest
+NiceString ns2 = getLongestNiceSubstring(s.substring(i + 1));%0A                if (ns1.nice && ns2.nice) %7B%0A                    if (ns1.string.length() %3E= ns2.string.length()) %7B%0A                        return new NiceString(ns1.string, true);%0A                    %7D else %7B%0A                        return new NiceString(ns2.string, true);%0A                    %7D%0A                %7D else if (ns1.nice) %7B%0A                    return ns1;%0A                %7D else if (ns2.nice) %7B%0A                    return ns2;%0A                %7D%0A                return new 
 NiceS
-ubs
 tring(%22
-aBbA%22)); // %22aBbA%22%0A        System.out.println(prob.longest
+%22, false);%0A            %7D%0A        %7D%0A        return new 
 NiceS
-ubs
 tring(
-%22aBbAa%22)); // %22aBbAa%22
+s, true);
 %0A   

Solve problem 1763