Find and Replace Pattern

Title: Find and Replace Pattern Source: leetcode.com You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get ...

Linked List Components

Title: Linked List Components Source: leetcode.com We are given head, the head node of a linked list containing unique integer values. We are also given the list G, a subset of the values in the linked list. Return the number of connected components in G, where two values are connected if they appear consecutively in ...

Increasing Order Search Tree

Title: Increasing Order Search Tree Source: leetcode.com Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child (Increasing order search tree). Example 1: Input: [5,3,6,2,4,null,8,1,null,null,null,7,9] 5 / \ 3 6 ...

Find all Anagrams in a String 2

Title: Custom Sort String Source: leetcode.com Given a string s and a non-empty string p, find all the start indices of p‘s anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter. Example ...

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. ...