LeetCode

953. Verifying an Alien Dictionary

In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language. Example 1: Input: words = [“hello”,”leetcode”], …

953. Verifying an Alien Dictionary Read More »

973. K Closest Points to Origin

Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0). The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 – x2)2 + (y1 – y2)2). You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in). Example …

973. K Closest Points to Origin Read More »

1650. Lowest Common Ancestor of a Binary Tree III

Given two nodes of a binary tree p and q, return their lowest common ancestor (LCA). Each node will have a reference to its parent node. The definition for Node is below: class Node { public int val; public Node left; public Node right; public Node parent; } According to the definition of LCA on Wikipedia: “The lowest common ancestor of two nodes p …

1650. Lowest Common Ancestor of a Binary Tree III Read More »

1570. Dot Product of Two Sparse Vectors

Given two sparse vectors, compute their dot product. Implement class SparseVector: SparseVector(nums) Initializes the object with the vector nums dotProduct(vec) Compute the dot product between the instance of SparseVector and vec A sparse vector is a vector that has mostly zero values, you should store the sparse vector efficiently and compute the dot product between two SparseVector. Follow up: What if only one of the vectors is sparse? …

1570. Dot Product of Two Sparse Vectors Read More »

680. Valid Palindrome II

Given a string s, return true if the s can be palindrome after deleting at most one character from it. Example 1: Input: s = “aba” Output: true Example 2: Input: s = “abca” Output: true Explanation: You could delete the character ‘c’. Example 3: Input: s = “abc” Output: false Constraints: 1 <= s.length <= 105 s consists of lowercase English letters. …

680. Valid Palindrome II Read More »

1249. Minimum Remove to Make Valid Parentheses

Given a string s of ‘(‘ , ‘)’ and lowercase English characters. Your task is to remove the minimum number of parentheses ( ‘(‘ or ‘)’, in any positions ) so that the resulting parentheses string is valid and return any valid string. Formally, a parentheses string is valid if and only if: It is the empty string, contains only lowercase characters, or It can be written as AB (A concatenated with B), where A and B are …

1249. Minimum Remove to Make Valid Parentheses Read More »

22. Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example 1: Input: n = 3 Output: [“((()))”,”(()())”,”(())()”,”()(())”,”()()()”] Example 2: Input: n = 1 Output: [“()”] Constraints: 1 <= n <= 8 p is the parenthesis-string built so far, left and right tell the number of left and right parentheses still to add, and parens collects the parentheses. Solution 1 …

22. Generate Parentheses Read More »

Scroll to Top