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

Power of Three

Title: Power of Three Source: leetcode.com Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion? Java solution Java /*https://leetcode.com/problems/power-of-three/*/ public class PowerOfThree { public boolean isPowerOfThree(int n) { if(n==0) return false; while(n!=1) { //System.out.println(n); int remainder = ...

Delete Node in a Linked List

Title: Delete Node in a Linked List Source: leetcode.com Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Suppose the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list ...

Decode Ways2

Title: Decode Ways Source: leetcode.com A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 1234 'A' -> 1'B' -> 2...'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, ...

Verify Preorder Serialization of a Binary Tree4

Title: Verify Preorder Serialization of a Binary Tree Source: leetcode.com One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ ...