2
public String longestNiceSubstring(String s) {4
// take first index, go from 0 to length-1 of the string5
for (int i = 0; i < s.length(); i++) {6
// take second index, this should go up to the length of the string <=7
for (int j = i + 1; j <= s.length(); j++) {8
// get the substring for the index range from i to j9
String temp = s.substring(i, j);10
// if length of the substring should be greater than 111
// if the length should be greater that the previous computed result12
// if the substring is valid Nice String13
// then update the result with the current substring from range i and j14
if (temp.length() > 1 && result.length() < temp.length() && checkNice(temp)) result = temp;20
// validate Nice String check21
public boolean checkNice(String temp) {22
// add substring to the set23
Set<Character> s = new HashSet<>();24
for (char ch : temp.toCharArray()) s.add(ch);26
// return false If you do not find both lower case and upper case in the sub string27
// for e.g 'aAa' substring added to set will have both a and A in the substring which is valid28
// 'azaA' substring will fail for 'z'29
// 'aaaaaaaa' will return "" as result30
// make sure that the substring contains both lower and upper case32
if (s.contains(Character.toUpperCase(ch)) != s.contains(Character.toLowerCase(ch)))