g1701_1800.s1763_longest_nice_substring.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g1701_1800.s1763_longest_nice_substring;
// #Easy #String #Hash_Table #Bit_Manipulation #Sliding_Window
// #2022_04_30_Time_5_ms_(61.88%)_Space_43.7_MB_(27.43%)
import java.util.HashSet;
import java.util.Set;
/**
* 1763 - Longest Nice Substring\.
*
* Easy
*
* 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.
*
* **Constraints:**
*
* * `1 <= s.length <= 100`
* * `s` consists of uppercase and lowercase English letters.
**/
public class Solution {
public String longestNiceSubstring(String s) {
int index = isNotNiceString(s);
if (index == -1) {
return s;
}
String left = longestNiceSubstring(s.substring(0, index));
String right = longestNiceSubstring(s.substring(index + 1));
return left.length() >= right.length() ? left : right;
}
private int isNotNiceString(String s) {
Set set = new HashSet<>();
for (char c : s.toCharArray()) {
set.add(c);
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!set.contains(Character.toLowerCase(c))
|| !set.contains(Character.toUpperCase(c))) {
return i;
}
}
return -1;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy