Posts

Showing posts from January, 2025

A 10-year-old problem

Image
Apparently back in 2015 (10 years ago!) I tried to solve this problem several times, unsuccessfully. I was young and inexperienced, probably. I gave it another try now. The idea is to not have any sort of matrix. Basically, just keep track of the rows and have a hash table storing the strings in each row. As you move the row index, and you append the characters to the end of the strings, everything falls into place quite well. There is one odd case, when the number of rows is one, which doesn't really work in my calculations, hence I handle it separately in the beginning. Problem solved, only took 10 years. Code is down below, cheers, ACC. Zigzag Conversion - LeetCode The string  "PAYPALISHIRING"  is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line:  "PAHNAPLSIIGYIR" Write the code that will take a string a...

Prefix Sum VII

Image
Very easy problem which could have been solved in N^2 since N = 100, but you can also use Prefix Sum technique here, which beats 100% in speed and memory. Code is down below, cheers, ACC. Count Partitions with Even Sum Difference - LeetCode You are given an integer array  nums  of length  n . A  partition  is defined as an index  i  where  0 <= i < n - 1 , splitting the array into two  non-empty  subarrays such that: Left subarray contains indices  [0, i] . Right subarray contains indices  [i + 1, n - 1] . Return the number of  partitions  where the  difference  between the  sum  of the left and right subarrays is  even .   Example 1: Input:   nums = [10,10,3,7,6] Output:   4 Explanation: The 4 partitions are: [10] ,  [10, 3, 7, 6]  with a sum difference of  10 - 26 = -16 , which is even. [10, 10] ,  [3, 7, 6]  with a sum difference of  20 - 1...

Standard Simplified RegEx Implementation II

A simple modification to the code shown in the previous post can accommodate a new capability in the RegEx. Suppose that you want to add a new capability to the RegEx: given a digit N in the RegEx (from 0-9), match any number of N characters in the input. For example, for the RegEx: "a2c", the following are valid matches: 1. "abbc" 2. "cccc" Whereas the string "abbbc" would be an invalid since between the 'a' and 'c' there are 3 characters instead of 2. The change in the code to implement this functionality in highlighted here . You can see that the structure of this code allows us relatively easily to add new functionalities to the RegEx. This is one of them, but one can imagine others such as "matching a minimum of N characters" instead of exactly N characters. Hope it is helpful - cheers, ACC. private bool HasMatch(string str, string regex, int strEndIndex, ...

Standard Simplified RegEx Implementation

Image
Although this problem constraints the regex to one "*", it is simple to extend the solution to multiple stars. Two important points to consider here: 1. Instead of doing string manipulation, just pass and manipulate indexes 2. Think about the base case and the induction case Time seems to be a higher side due to the recursion and extrapolation to handling multiple stars. Code is down below, cheers, ACC. Substring Matching Pattern - LeetCode You are given a string  s  and a pattern string  p , where  p  contains  exactly one   '*'  character. The  '*'  in  p  can be replaced with any sequence of zero or more characters. Return  true  if  p  can be made a  substring  of  s , and  false  otherwise.   Example 1: Input:   s = "leetcode", p = "ee*e" Output:   true Explanation: By replacing the  '*'  with  "tcod" , the substring  "eetcode"  matches th...