my image

Dmitrii Volyx

Performance Engineer

Group Anagrams

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] Note: All inputs will be in lowercase. The order of your output does not matter. Solution class Solution { public List<List<String>> groupAnagrams(String[] strs) { Map<String, List<String>> map = new HashMap<>(); int[] c = new int[26]; for (String s : strs) { Arrays.fill(c, 0); for (int i = 0 ; i < s.length(); i++) { c[s.charAt(i) - 'a']++; } String code = Arrays.toString(c); List<String> list = map.get(code); if (list == null) { list = new ArrayList<>(); } list.add(s); map.put(code, list); } return new ArrayList<>(map.values()); } } Solution 2021-11-15 class Solution { public List<List<String>> groupAnagrams(String[] strs) { Map<String, List<String>> keyToList = new HashMap<>(); for (String s: strs) { char[] charString = s.toCharArray(); Arrays.sort(charString); String sortedWord = String.valueOf(charString); List<String> anagrams = keyToList.getOrDefault(sortedWord, new ArrayList<>()); anagrams.add(s); keyToList.put(sortedWord, anagrams); } List<List<String>> res = new ArrayList<>(); for (var anagrams: keyToList.values()) { res.add(anagrams); } return res; } }

May 14, 2020 · 1 min · volyx

Binary Tree Maximum Path Sum

Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root. Example 1: Input: [1,2,3] 1 / \ 2 3 Output: 6 Example 2: ...

May 13, 2020 · 2 min · volyx

Maximal Square

Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 Solution class Solution { public int maximalSquare(char[][] matrix) { int n = matrix.length; if (n == 0) { return 0; } int m = matrix[0].length; int[][] dp = new int[n][m]; int answer = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (matrix[i][j] == '1') { dp[i][j] = 1; if (i > 0 && j > 0) { dp[i][j] += Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])); } answer = Math.max(answer, dp[i][j]); } } } return answer * answer; } }

May 12, 2020 · 1 min · volyx

Longest Common Subsequence

Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, “ace” is a subsequence of “abcde” while “aec” is not). A common subsequence of two strings is a subsequence that is common to both strings. If there is no common subsequence, return 0. ...

May 9, 2020 · 3 min · volyx

Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: nums = [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: nums = [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index. Constraints: ...

May 5, 2020 · 2 min · volyx

LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. ...

May 3, 2020 · 2 min · volyx

Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree

Given a binary tree where each path going from the root to any leaf form a valid sequence, check if a given string is a valid sequence in such binary tree. We get the given string from the concatenation of an array of integers arr and the concatenation of all values of the nodes along a path results in a sequence in the given binary tree. Example 1: Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,0,1] Output: true Explanation: The path 0 -> 1 -> 0 -> 1 is a valid sequence (green color in the figure). Other valid sequences are: 0 -> 1 -> 1 -> 0 0 -> 0 -> 0 Example 2: ...

May 1, 2020 · 2 min · volyx

Bitwise AND of Numbers Range

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. Example 1: Input: [5,7] Output: 4 Example 2: Input: [0,1] Output: 0 Solution 1 class Solution { public int rangeBitwiseAnd(int m, int n) { String left = Integer.toString(m, 2); String right = Integer.toString(n, 2); if (left.length() != right.length()) { return 0; } int i = 0; int counter = 1; StringBuilder sb = new StringBuilder(); while (i < left.length()) { if (left.charAt(i) == right.charAt(i)) { sb.append(left.charAt(i)); } else { while (i < left.length()) { sb.append('0'); i++; } } i++; } return Integer.parseInt(sb.toString(), 2); } } Solution 2 ...

April 30, 2020 · 1 min · volyx

560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7]. Hints ...

April 28, 2020 · 2 min · volyx

Leftmost Column with at Least a One

(This problem is an interactive problem.) A binary matrix means that all elements are 0 or 1. For each individual row of the matrix, this row is sorted in non-decreasing order. Given a row-sorted binary matrix binaryMatrix, return leftmost column index(0-indexed) with at least a 1 in it. If such index doesn’t exist, return -1. You can’t access the Binary Matrix directly. You may only access the matrix using a BinaryMatrix interface: ...

April 26, 2020 · 2 min · volyx