Created
August 13, 2023 18:56
-
-
Save primaryobjects/4cdb3953ffa5c2644c98bdb568e92f6f to your computer and use it in GitHub Desktop.
Longest Nice Substring containing uppercase and lowercase matching letters https://leetcode.com/problems/longest-nice-substring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const longestNiceSubstring = s => { | |
| // Brute-force. | |
| let result = ''; | |
| let max = 0; | |
| const adder = 32; // uppercase or lowercase compliment | |
| // Begin building the word from the left and appending 1 character at a time. | |
| for (let i=0; i<s.length-1; i++) { | |
| const hash = {}; | |
| let word = s[i]; | |
| hash[s[i]] = 1; | |
| // Append 1 character to the word. | |
| for (let j=i+1; j<s.length; j++) { | |
| word += s[j]; | |
| hash[s[j]] = 1; | |
| // Check if this word is valid. | |
| let isValid = true; | |
| for (let j=0; j<word.length; j++) { | |
| const ch = word[j].charCodeAt(0); | |
| const compliment = String.fromCharCode(ch + (ch <= 95 ? adder : -adder)); | |
| if (!hash[word[j]] || !hash[compliment]) { | |
| // Invalid word, but keep appending characters to see if it becomes valid. | |
| isValid = false; | |
| break; | |
| } | |
| } | |
| if (isValid) { | |
| // This is a valid word, check if it is the largest. | |
| if (word.length > max) { | |
| result = word; | |
| max = result.length; | |
| } | |
| } | |
| } | |
| if (s.length - i <= max) { | |
| // Terminate early if no possible substring greater than what we've already found. | |
| break; | |
| } | |
| } | |
| return result; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function longestNiceSubstring(s) { | |
| // Recursion. | |
| if (s.length < 2) return ""; | |
| const set = new Set(); | |
| for (const c of s) set.add(c); | |
| for (let i = 0; i <= s.length - 1; i++) { | |
| const upperCaseChar = s[i].toUpperCase(); | |
| const lowerCaseChar = s[i].toLowerCase(); | |
| if (set.has(upperCaseChar) && set.has(lowerCaseChar)) { | |
| continue; | |
| } | |
| var str1=longestNiceSubstring(s.substring(0,i)); | |
| var str2=longestNiceSubstring(s.substring(i+1)); | |
| return str1.length>=str2.length?str1:str2; | |
| } | |
| return s; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment