LeetCode – Rearrange String k Distance Apart (Java)

Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other. All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string “”. Example: str = “aabbcc”, k = 3 Result: … Read more

LeetCode – Design Twitter (Java)

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the following methods: postTweet(userId, tweetId): Compose a new tweet. getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user’s news feed. … Read more

LeetCode – Nested List Weight Sum II (Java)

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list — whose elements may also be integers or other lists. Different from the previous question where weight is increasing from root to leaf, now the weight is … Read more

LeetCode – Plus One Linked List (Java)

Given a non-negative number represented as a singly linked list of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. Example: Input: 1->2->3 Output: 1->2->4 Java Solution public ListNode plusOne(ListNode head) { ListNode h2 = reverse(head);   ListNode p=h2;   while(p!=null){ … Read more

LeetCode – Design Phone Directory (Java)

Design a Phone Directory which supports the following operations: get: Provide a number which is not assigned to anyone. check: Check if a number is available or not. release: Recycle or release a number. Java Solution 1 public class PhoneDirectory { int max; HashSet<Integer> set; LinkedList<Integer> queue;   /** Initialize your data structure here @param … Read more

LeetCode – Shuffle an Array (Java)

Shuffle a set of numbers without duplicates. Java Solution How we make sure each the probability of each element get shuffled is very similar to the streaming random problem. The algorithm is straightforward to understand, but the question is why it works. To have a working shuffle algorithm, every element in the array results in … Read more

LeetCode – Linked List Random Node (Java)

Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space? Java Solution This problem is … Read more

LeetCode – Insert Delete GetRandom O(1) – Duplicates allowed (Java)

Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the collection. remove(val): Removes an item val from the collection if present. getRandom(): Returns a random element from current collection of elements. The probability of each element being returned is linearly … Read more

LeetCode – Insert Delete GetRandom O(1) (Java)

Design a data structure that supports all following operations in O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item val from the set if present. getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned. Java … Read more