LeetCode – Expressive Words (Java)

Sometimes people repeat letters to represent extra feeling, such as “hello” -> “heeellooo”, “hi” -> “hiiii”. In these strings like “heeellooo”, we have groups of adjacent letters that are all the same: “h”, “eee”, “ll”, “ooo”. For some given string S, a query word is stretchy if it can be made to be equal to … Read more

LeetCode – Find And Replace in String (Java)

To some string S, we will perform some replacement operations that replace groups of letters with new ones (not necessarily the same size). Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original … Read more

LeetCode – Wiggle Sort (Java)

Given an unsorted array nums, reorder it in-place such that nums[0] < nums[1] > nums[2] < nums[3].... Example: Input: nums = [3,5,2,1,6,4] Output: One possible answer is [3,5,1,6,2,4] Java Solution public void wiggleSort(int[] nums) { if (nums == null || nums.length <= 1) { return; }   for (int i = 1; i < nums.length; ... Read more

LeetCode – Missing Ranges (Java)

Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges. Example: Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99, Output: [“2”, “4->49”, “51->74”, “76->99”] Java Solution public List<String> findMissingRanges(int[] nums, int lower, int upper) { List<String> … Read more

LeetCode – Partition Labels (Java)

A string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts. For example: Input: S = “ababfeefhijkh” Output: [4,4,5] Explanation: The partition is “abab”, … Read more

Java – Sort Map By Value

In Java, we can use the TreeMap class to sort a map by its keys. This class is very handy to use. However, sometimes we need to sort a map by its values. How to sort a map by its values is a most frequently asked question by Java programmers. In this post, I will … Read more

HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap

Map is one of the most important data structures in Java. In this post, I will illustrate how to use different types of maps, such as HashMap, TreeMap, HashTable and LinkedHashMap. 1. Map Overview There are 4 commonly used implementations of Map in Java SE – HashMap, TreeMap, Hashtable and LinkedHashMap. If we use only … Read more

HashSet vs. TreeSet vs. LinkedHashSet

A Set contains no duplicate elements. That is one of the major reasons to use a set. There are 3 commonly used implementations of Set: HashSet, TreeSet and LinkedHashSet. When and which to use is an important question. In brief, if you need a fast set, you should use HashSet; if you need a sorted … Read more

LeetCode – LRU Cache (Java)

Design and implement a data structure for Least Recently Used (LRU) cache, which supports get and put. Analysis The key to solve this problem is using a double linked list which enables us to quickly move nodes. The LRU cache is a hash table of keys and double linked nodes. The hash table makes the … Read more

LeetCode – Palindrome Partitioning (Java)

Problem Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = “aab”, Return [ [“aa”,”b”], [“a”,”a”,”b”] ] 1. Depth-first Search public ArrayList<ArrayList<String>> partition(String s) { ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();   if (s == null || s.length() … Read more

Longest Substring with At Most K Distinct Characters

This is a problem asked by Google. Given a string, find the longest substring that contains only two unique characters. For example, given “abcbbbbcccbdddadacb”, the longest substring that contains 2 unique character is “bcbbbbcccb”. 1. Longest Substring Which Contains 2 Unique Characters In this solution, a hashmap is used to track the unique elements in … Read more

LeetCode – Palindrome Number (Java)

Determine whether an integer is a palindrome. Do this without extra space. Thoughts Problems related with numbers are frequently solved by / and %. No need of extra space is required. This problem is similar with the Reverse Integer problem. Note: no extra space here means do not convert the integer to string, since string … Read more