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. Input: s = "bcabc" Output: "abc" Convert the string to an array of char, and store it in a LinkedHashSet . This will preserve your ordering, and remove duplicates. Something like: class Solution { public String removeDuplicateLetters(String s) { String string = "bcabc"; char[] chars = string.toCharArray(); Arrays.sort(chars); Set<Character> charSet = new LinkedHashSet<Character>(); for (char c : chars) { charSet.add(c); } StringBuilder sb = new StringBuilder(); for (Character character : charSet) { sb.append(character); } System.out.println(sb.toString()); return sb.toString(); } }
Thread Pool A thread pool reuses previously created threads to execute current tasks and offers a solution to the problem of thread cycle overhead and resource thrashing. It is a software design pattern for achieving concurrency of execution in a computer program. Since the thread is already existing when the request arrives, the delay introduced by thread creation is eliminated, making the application more responsive. The size of a thread pool is the number of threads kept in reserve for executing tasks What does Static word in Java mean? Can we override a Static method? A member of a class that can be accessed without instantiating the object of the class. We cannot override static method in java since overriding is based on dynamic binding at runtime and static members are bound at compile time Can abstract class have static methods? If we declare a method in a class abstract to use it, we must override this method in the subclass. But,...
How to Merge Two Arrays in Java There are following ways to merge two arrays: Java arraycopy() method Without using arraycopy() method Java Collections Java arraycopy() method public static void arraycopy(Object source, int source_position, Object destination, int destination_position, int length) Parameters source : It is a source array. source_position : Starting point in the source array. destination : It is a destination array. destination_position : Starting position in the destination array. length : The number of array elements to be copied import java.util.Arrays; public class MergeArrayExample1 { public static void main(String[] args) { int [] firstArray = { 23 , 45 , 12 , 78 , 4 , 90 , 1 }; //so...
Comments
Post a Comment