my image

Dmitrii Volyx

I’m Dmitrii, a Senior Software Engineer at Meta working on Wearables Performance.
I write about software engineering, performance, and open-source.

Complement of Base 10 Integer

Every non-negative integer N has a binary representation. For example, 5 can be represented as “101” in binary, 11 as “1011” in binary, and so on. Note that except for N = 0, there are no leading zeroes in any binary representation. The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1. For example, the complement of “101” in binary is “010” in binary. ...

May 25, 2020 · 2 min · volyx

Cousins in Binary Tree

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1. Two nodes of a binary tree are cousins if they have the same depth, but have different parents. We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree. Return true if and only if the nodes corresponding to the values x and y are cousins. ...

May 25, 2020 · 2 min · volyx

First Unique Character in a String

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1. Examples: 1 2 3 4 5 s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase letters. Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public int firstUniqChar(String s) { Map<Character, Integer> freq = new HashMap<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); freq.put(c, freq.getOrDefault(c, 0) + 1); } for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (freq.get(c) == 1) { return i; } } return -1; } }

May 25, 2020 · 1 min · volyx

Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1: 1 2 Input: [3,2,3] Output: 3 Example 2: 1 2 Input: [2,2,1,1,1,2,2] Output: 2 Solution: ...

May 25, 2020 · 1 min · volyx

Guess Number Higher or Lower

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each letter in the magazine string can only be used once in your ransom note. Example 1: 1 2 Input: ransomNote = "a", magazine = "b" Output: false Example 2: ...

May 22, 2020 · 1 min · volyx

First Bad Version

You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels. The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so “a” is considered a different type of stone from “A”. ...

May 20, 2020 · 1 min · volyx

First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad. ...

May 19, 2020 · 3 min · volyx

Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements. Example 1: 1 2 3 4 5 6 7 Input: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \ 2 Output: 1 Example 2: ...

May 18, 2020 · 2 min · volyx

Palindromic Substrings

Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. Example 1: 1 2 3 Input: "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c". Example 2: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 Input: "aaa" Output: 6 Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa". ``` Note: ``` The input string length won't exceed 1000. ``` Solution ```java class Solution { public int countSubstrings(String s) { int n = s.length(); int[][] a = new int[n][n]; int count = 0; for (int i = 0; i < n; i++) { a[i][i] = 1; count++; } for (int col = 1; col < n; col++) { for (int row = 0; row < col; row++) { if (row == col - 1 && s.charAt(col) == s.charAt(row)) { a[row][col] = 1; count++; } else if (a[row + 1][col - 1] == 1 && s.charAt(col) == s.charAt(row) ) { a[row][col] = 1; count++; } } } return count; } } ``` ![example](/images/2020-05-15-palindromic-substring_1_optimized.png) ![example](/images/2020-05-15-palindromic-substring-matrix_optimized.png)

May 15, 2020 · 2 min · volyx

Group Anagrams

Given an array of strings, group anagrams together. Example: 1 2 3 4 5 6 7 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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 · 2 min · volyx