Custom Sort String

Title: Custom Sort String Source: leetcode.com S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before ...

Monotonic Array

Title: Monotonic Array Source: leetcode.com An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j]. Return true if and only if the ...

Nth Digit in Whole Numbers

Title: Nth DigitSource: leetcode.com Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, … Note: n is positive and will fit within the range of a 32-bit signed integer (n < 231). Python solution The intuition for the solution is thinking about what’s the ...

Binary Watch Problem

Title: Binary Watch Source: leetcode.com A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right. For example, the above binary watch reads “3:25”. Given a ...

Leaf-Similar trees

Title: Leaf-Similar TreesSource: leetcode.com Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence. For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8). Two binary trees are considered leaf-similar if their leaf value sequence ...

Shortest Distance to a Character

Title: Shortest Distance to a CharacterSource: leetcode.com Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = “loveleetcode”, C = ‘e’ Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0] Note: 1. ...

Student Attendance Record I

Title: Positions of Large Groups Source: leetcode.com You are given a string representing an attendance record for a student. The record only contains the following three characters: ‘A’ : Absent. ‘L’ : Late. ‘P’ : Present. A student could be rewarded if his attendance record doesn’t contain more than one ‘A’ (absent) or more than ...

Positions of Large Groups

Title: Positions of Large Groups Source: leetcode.com In a string S of lowercase letters, these letters form consecutive groups of the same character. For example, a string like S = “abbxxxxzyy” has the groups “a”, “bb”, “xxxx”, “z” and “yy”. Call a group large if it has 3 or more characters. We would like the ...

N-ary Tree PreOrder Traversal 2

Title: N-ary Tree Preorder Traversal Source: leetcode.com Given an n-ary tree, return the preorder traversal of its nodes’ values. For example, given a 3-ary tree: Return its preorder traversal as: [1,3,5,6,2,4]. Note: Recursive solution is trivial, could you do it iteratively? You could find another explanation/ problem related to Preorder Traversal here. Java solution (Recursive) ...

Image Smoother

Title: Image SmootherSource: leetcode.com Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, ...