Posts

Word Pattern

Given a pattern and a string s , find if s  follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s . Example 1: Input: pattern = "abba", s = "dog cat cat dog" Output: true Example 2: Input: pattern = "abba", s = "dog cat cat fish" Output: false Example 3: Input: pattern = "aaaa", s = "dog cat cat dog" Output: false Example 4: Input: pattern = "abba", s = "dog dog dog dog" Output: false   Constraints: 1 <= pattern.length <= 300 pattern contains only lower-case English letters. 1 <= s.length <= 3000 s contains only lower-case English letters and spaces ' ' . s does not contain any leading or trailing spaces. All the words in s are separated by a single space .   class Solution {     public boolean wordPattern(String pattern, String s) {         H

Detect Capital

  Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cases holds: All letters in this word are capitals, like "USA". All letters in this word are not capitals, like "leetcode". Only the first letter in this word is capital, like "Google". Otherwise, we define that this word doesn't use capitals in a right way.   Example 1: Input: "USA" Output: True   Example 2: Input: "FlaG" Output: False   Note:  The input will be a non-empty word consisting of uppercase and lowercase latin letters.   class Solution {     public boolean detectCapitalUse(String word) {         int length = word.length(), i, upCount=0, flag=0, lowCount = 0;         for(i=0;i<length;i++){             if(Character.isUpperCa

Flatten a Multilevel Doubly Linked List

Image
You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below. Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.   Example 1: Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12] Output: [1,2,3,7,8,11,12,9,10,4,5,6] Explanation: The multilevel linked list in the input is as follows: After flattening the multilevel linked list it becomes: Example 2: Input: head = [1,2,null,3] Output: [1,3,2] Explanation: The input multilevel linked list is as follows: 1---2---NULL | 3---NULL Example 3: Input: head = [] Output: []   How multilevel linked list is represented in test case: We use the

Maximum Width of Binary Tree

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree , but some nodes are null. The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation. Example 1: Input: 1 / \ 3 2 / \ \ 5 3 9 Output: 4 Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9). Example 2: Input: 1 / 3 / \ 5 3 Output: 2 Explanation: The maximum width existing in the third level with the length 2 (5,3). Example 3: Input: 1 / \ 3 2 / 5 Output: 2 Explanation: The maximum width

3Sum

Given an array nums of n integers, are there elements a , b , c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]   class Solution {     public List<List<Integer>> threeSum(int[] nums) {         if (nums.length < 3) return new ArrayList<>();         Arrays.sort(nums);         Set<List<Integer>> triplet= new HashSet<>();         for (int i = 0; i < nums.length - 2; i++) {             int j = i + 1;             int k = nums.length - 1;             while (j < k) {                 int sum = nums[i] + nums[j] + nums[k];                 if (sum == 0) triplet.add(Arrays.asList(nums[i], nums[j++], nums[k--]));                 else if (sum > 0) k--;                 else if (sum < 0) j++;             }   

Island Perimeter

Image
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island. Example: Input: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] Output: 16 Explanation: The perimeter is the 16 yellow stripes in the image below:   class Solution {     public int islandPerimeter(int[][] grid) {         int lands = 0, neighbours = 0;         for (int i = 0; i < grid.length; i++) {             for (int j = 0; j < grid[i].length; j++) {                 if (grid[i][j] == 1) {