Q316 Remove Duplicate Letters
Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Example 1:
Input: s = "bcabc"
Output: "abc"
Example 2:
Input: s = "cbacdcbc"
Output: "acdb"
Constraints:
1 <= s.length <= 10<sup>4</sup>sconsists of lowercase English letters.
解法及注释
class Solution {
public String removeDuplicateLetters(String s) {
//pre-check
if(s == null || s.length() == 0)
return "";
//get count number for each letter
int[] count = new int[26];
for(char c : s.toCharArray())
count[c-'a']++;
//keep track of visited letter to avoid duplicate in result
boolean[] visited = new boolean[26];
Stack<Character> res = new Stack();
for(char c : s.toCharArray()) {
count[c - 'a']--;
if(visited[c-'a'])
continue;
//traverse through the stack and check for larger characters
//if found and it is not the last position then pop from stack
//Eg: bcabc => if stack has bc, now a<b and curr b is not the last one
//if not in last position come out of loop and add curr character to stack
while(!res.isEmpty() && c < res.peek() && count[res.peek() - 'a'] > 0) visited[res.pop() - 'a'] = false;
res.push(c);
visited[c - 'a'] = true;
}
StringBuilder sb = new StringBuilder();
while(!res.isEmpty())
sb.append(res.pop());
return sb.reverse().toString();
}
}
Q1081 Smallest Subsequence of Distinct Characters
Return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.
Example 1:
Input: s = "bcabc"
Output: "abc"
Example 2:
Input: s = "cbacdcbc"
Output: "acdb"
Constraints:
1 <= s.length <= 1000sconsists of lowercase English letters.
解法及注释
class Solution {
public String smallestSubsequence(String s) {
//pre-check
if(s == null || s.length() == 0)
return "";
int[] count = new int[26];
Stack<Character> stack = new Stack();
for(char c : s.toCharArray())
count[c - 'a']++;
//used to check if the char has been visited or not
boolean[] visited = new boolean[26];
for(char c : s.toCharArray()) {
count[c - 'a']--;
if(visited[c - 'a'])
continue;
//in case that c is smaller than last element in stack
//and it is not the last in count array
//pop out existing ones in stack and reset the vistied array
while(!stack.isEmpty() && c < stack.peek() && count[stack.peek() - 'a'] > 0)
visited[stack.pop() - 'a'] = false;
stack.push(c);
visited[c - 'a'] = true;
}
StringBuilder sb = new StringBuilder();
while(!stack.isEmpty())
sb.append(stack.pop());
return sb.reverse().toString();
}
}
Q1016 Binary String With Substrings Representing 1 To N
Given a binary string S (a string consisting only of '0' and '1's) and a positive integer N, return true if and only if for every integer X from 1 to N, the binary representation of X is a substring of S.
Example 1:
Input: S = "0110", N = 3
Output: true
Example 2:
Input: S = "0110", N = 4
Output: false
解法及注释
class Solution {
public boolean queryString(String S, int N) {
for(int i = 1; i <= N; i++)
if(S.indexOf(Integer.toBinaryString(i)) == -1)
return false;
return true;
}
}