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

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

Matrix Transpose

Title: Transpose MatrixSource: leetcode.com Code to return transpose of a matrix. Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it’s main diagonal, switching the row and column indices of the matrix. Example 1: Input: [[1,2,3],[4,5,6],[7,8,9]] Output: [[1,4,7],[2,5,8],[3,6,9]] Example 2: Input: [[1,2,3],[4,5,6]] Output: [[1,4],[2,5],[3,6]] Python ...

Longest Palindrome

Title: Longest PalindromeSource: leetcode.com Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example “Aa” is not considered a palindrome here. Note: Assume the length of given string will not exceed 1,010. Example 1: Input: ...

Goat Latin

Title: Goat Latin Source: leetcode.com A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. We would like to convert the sentence to “Goat Latin” (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows: If a word begins with ...

Buddy Strings

Title: Buddy Strings Source: leetcode.com Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B. Example 1: Input: A = “ab”, B = “ba” Output: true Example 2: Input: A = “ab”, B = “ab” Output: false ...

Maximum Product of Word Lengths

Title: Maximum Product of Word Lengths Source: leetcode.com Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example 1: Input: [“abcw”,”baz”,”foo”,”bar”,”xtfn”,”abcdef”] Output: ...

Water and Jug Problem

Title: Water and Jug Problem Source: leetcode.com You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs. If z liters of water is measurable, you must have z ...