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(); } }
What is Stack? Stack is a linear data structure that follows the specific order to perform the operations. For example, if we want to access the element in the array we can do it any time but in the case of stack data structure, there is only one sequence to access the element. In the stack, we insert the element from one end with push operation and delete the element from the same end using pop operation. The end of the stack used to perform all the operations is called the top of the stack. Therefore, a stack follows the LIFO (Last In First Out) principle, which means the element that is inserted last will be the first element to come out of the stack. The most important thing to remember in the stack data structure is that it stores the elements of the same data type only. Let us understand the condition to check whether the stack is empty for full: Condition to Check if Stack is Empty int Empty() { if (top ==- 1 ) return 1 ; else return 0 ; } Con...
Kotlin is a statically typed, open-source programming language that runs on the JVM and can be used anywhere Java is used today. It can be compiled using Java source code The most popular features of kotlin are: Kotlin is Concise: reduces the writing of the extra codes Compact code: Kotlin is an OOPs-based programming language. Less number of code compared to java. Kotlin is Simple: Compiling the code is simple, resulting in improved performance for Android development. Null safety: Kotlin is a null safety language. It aims to eliminate the NullPointerException (null reference) from the code. Java Interoperability: Kotlin provides full interoperability for Java code. Java code can utilize Kotlin code, and Kotlin code can use Java code. Compilation Time: Kotlin is faster and better than Java in terms of its performance and fast compilation time. Default & Named Parameters Difference between Val and Constant Both val and const ...
Comments
Post a Comment