Posts

Showing posts from August, 2023

Permutation in String: 6 years later...

Image
Wow, can't believe I've been trying to solve this problem for years, to be precise 6 years. Key is to not use a Hashtable (too expensive for this case) but rather a 26-len array. That brings the complexity to 26x10^4, totally doable. Code is down below, cheers, ACC. Permutation in String - LeetCode 567. Permutation in String Medium 10523 345 Add to List Share Given two strings  s1  and  s2 , return  true  if  s2  contains a permutation of  s1 , or  false  otherwise . In other words, return  true  if one of  s1 's permutations is the substring of  s2 .   Example 1: Input: s1 = "ab", s2 = "eidbaooo" Output: true Explanation: s2 contains one permutation of s1 ("ba"). Example 2: Input: s1 = "ab", s2 = "eidboaoo" Output: false   Constraints: 1 <= s1.length, s2.length <= 10 4 s1  and  s2  consist of lowercase English letters. Accepted 710,484 Submissions 1,608,202 public bool CheckInclusion(string s1, string s2) { i

One liner using LINQ

Image
Don't recommend solving this problem this way, but it is doable with one liner using LINQ. Code is down below, cheers, ACC. Furthest Point From Origin - LeetCode 2833. Furthest Point From Origin Easy 96 15 Add to List Share You are given a string  moves  of length  n  consisting only of characters  'L' ,  'R' , and  '_' . The string represents your movement on a number line starting from the origin  0 . In the  i th  move, you can choose one of the following directions: move to the left if  moves[i] = 'L'  or  moves[i] = '_' move to the right if  moves[i] = 'R'  or  moves[i] = '_' Return  the  distance from the origin  of the  furthest  point you can get to after  n  moves .   Example 1: Input: moves = "L_RL__R" Output: 3 Explanation: The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR". Example 2: Input: moves = "_R__LL_" Output: 5 E

Linear time solution using "bucketization" approach

Image
Easy problem which could have been solved in N^2 since N=100, but for a very large N, say N=10^6, a square solution would have been intractable. Instead, make use of the "bucketization" approach: 1/ Have a bucket with 10 elements corresponding to the 10 available digits 2/ Each bucket element is actually the top two elements whose max digit is the bucket index That way you can populate the bucket in N*Log(max number in nums), since the max number in nums is 10^4, you have a time complexity of N*Log(10^4), which would be 4N which would be a big O(N)-time complexity. Code is down below, cheers, ACC. Max Pair Sum in an Array - LeetCode 2815. Max Pair Sum in an Array Easy 107 39 Add to List Share You are given a  0-indexed  integer array  nums . You have to find the  maximum  sum of a pair of numbers from  nums  such that the maximum  digit  in both numbers are equal. Return  the maximum sum or   -1  if no such pair exists .   Example 1: Input: nums = [51,71,17,24,42] Output: 8

Head Recursion

Image
In this problem a head-recursive solution is needed. Start the solution by calling the function recursively until the end of the list is reached. On the way back process the results, until you get to the head of the list (by checking the parent node being equal to null). Code is down below, cheers, ACC. Double a Number Represented as a Linked List - LeetCode 2816. Double a Number Represented as a Linked List Medium 167 0 Add to List Share You are given the  head  of a  non-empty  linked list representing a non-negative integer without leading zeroes. Return  the  head  of the linked list after  doubling  it .   Example 1: Input: head = [1,8,9] Output: [3,7,8] Explanation: The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. Example 2: Input: head = [9,9,9] Output: [1,9,9,8] Explanation: The figure above corresponds to the given linked list which represents the number 999. Hence,