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.

&nbsp;
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.


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 100
	s consists of uppercase and lowercase English letters.


class Solution:
    def longestNiceSubstring(self, s: str) -> str:
        def divcon(s):
		    # string with length 1 or less arent considered nice
            if len(s) < 2:
                return ""
            
            pivot = []
            # get every character that is not nice
            for i, ch in enumerate(s):
                if ch.isupper() and ch.lower() not in s:
                    pivot.append(i)
                if ch.islower() and ch.upper() not in s:
                    pivot.append(i)
			# if no such character return the string
            if not pivot:
                return s
			# divide the string in half excluding the char that makes the string not nice
            else:
                mid = (len(pivot)) // 2
                return max(divcon(s[:pivot[mid]]),divcon(s[pivot[mid]+1:]),key=len)
        
        return divcon(s)
class Solution {
    public String longestNiceSubstring(String s) {
        String result = "";
        // take first index, go from 0 to length-1 of the string
		for (int i = 0;i<s.length(); i++){        
            // take second index, this should go up to the length of the string <=
			for (int j = i+1;j<=s.length(); j++){
                //get the substring for the index range from i to j
				String temp = s.substring(i, j);
                // if length of the substring should be greater than 1
				// if the length should be greater that the previous computed result
				// if the substring is valid Nice String
				// then update the result with the current substring from range i and j
				if (temp.length() > 1 && result.length() < temp.length() && checkNice(temp)) result = temp;
            }    
        }
        return result;
    }
    
	//validate Nice String check
    public boolean checkNice(String temp){
        //add substring to the set
		Set<Character> s = new HashSet<>();
        for (char ch : temp.toCharArray()) s.add(ch);
        
		// return false If you do not find both lower case and upper case in the sub string
		//for e.g 'aAa' substring added to set will have both a and A in the substring which is valid
		// 'azaA' substring will fail for 'z'
		// 'aaaaaaaa' will return "" as result
		//make sure that the substring contains both lower and upper case
        for (char ch : s)
            if (s.contains(Character.toUpperCase(ch)) != s.contains(Character.toLowerCase(ch))) return false;  
        return true;
    }
}
class Solution {
public:
    
    string longestNiceSubstring(string s) {
        int arr1[26]={};
        int arr2[26]={};
        if(s.length()<2)
            return "";
        for(char ch:s)
        {
            if(ch>='A' && ch<='Z')
                arr1[(ch|32)-'a']++;
            else
                arr2[ch-'a']++;
        }
        vector<int> index;
        index.push_back(-1);
        for(int i=0;i<s.length();i++)
        {
            if((arr1[(s[i]|32)-'a']>=1 && arr2[(s[i]|32)-'a']==0) || (arr1[(s[i]|32)-'a']==0 && arr2[(s[i]|32)-'a']>=1))
                index.push_back(i);
        }
        //index.push_back(2);
        index.push_back(s.length());
        if(index.size()==2)
            return s;
        string minn="";
        for(int i=0;i<index.size()-1;i++)
        {
            string temp = longestNiceSubstring(s.substr(index[i]+1,index[i+1]-index[i]-1));
            minn = temp.length()>minn.length()?temp:minn;
        }
        return minn;
    }
};
const swapCase = (str) => str.split('').map((c) => c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase()).join('');

var longestNiceSubstring = function(s) {
    let ans = "";
        for (let i = 0; i < s.length; i++) {
            for (let ii = i + 1; ii < s.length + 1; ii++) {
                let substring = s.slice(i, ii); // we take a substring

                let invertedCaseChars = [...substring].map(swapCase); // we create an array of chars from the substring and invert case of this chars
                
                if (invertedCaseChars.every(char => substring.includes(char))) { // we check that substring includes every case inverted char (see the illustration above)
                    ans = substring.length > ans.length ? substring : ans; // we select the longest substring which satisfies our condition
                }
            } 
        }
        return ans 
};
Longest Nice Substring