1
const swapCase = (str) =>4
.map((c) => (c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase()))7
var longestNiceSubstring = function (s) {9
for (let i = 0; i < s.length; i++) {10
for (let ii = i + 1; ii < s.length + 1; ii++) {11
let substring = s.slice(i, ii); // we take a substring13
let invertedCaseChars = [...substring].map(swapCase); // we create an array of chars from the substring and invert case of this chars15
if (invertedCaseChars.every((char) => substring.includes(char))) {16
// we check that substring includes every case inverted char (see the illustration above)17
ans = substring.length > ans.length ? substring : ans; // we select the longest substring which satisfies our condition