Posts

StringBuilder VI

Image
Another problem where using String leads to a time limit exceeded, but StringBuilder (just for the token processing) does the trick. Just some calculation is needed for the characters rotation. Code is down below, cheers, ACC. Decrypt the Message - LeetCode You are given a string s representing an encrypted message consisting of one or more tokens. Each token consists of lowercase English letters, followed by '#' and an integer representing its encryption key. The tokens are separated by single spaces. To decrypt a token, shift each letter backward by the number of positions specified by its key. The alphabet wraps around, so shifting 'a' backward by one position produces 'z' . Return the decrypted message , with the keys removed and the decrypted words in their original order, separated by single spaces.   Example 1: Input: s = "abc#1" Output: "zab" Explanation: The token "abc#1" has a key of 1, so each letter is shifted ba...

Constraints

Image
In this problem it is important to understand the constraints: since we're dealing with domino numbers, there are only 6 options and there are only 2 positions (top and bottom) hence you can create one function to check whether you can achieve the goal for one number with one specific position, leading to 12 variations, since the size of the arrays is O(10^4) then we end up with the same order of magnitude with the constant slightly higher. Code is down below, cheers, ACC. Minimum Domino Rotations For Equal Row - LeetCode In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the i th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.) We may rotate the i th domino, so that tops[i] and bottoms[i] swap values. Return the minimum number of rotations so that all the values in tops are the same, or all the values in bottoms are the same. If it cannot be done, return -1 .   Example 1: Input: tops = [2,1,2,4,2,2]...

On the Zeroes of the Riemann Zeta Function using Von Mangoldt function

Image
Recently I was watching a Numberphile about the solution to the Riemann Hypothesis (RH). This was the video:  EET324 Class 24 One thing intrigued me when the author was talking about the explanation of the RH using the Von Mangoldt function, in particular I saw this part of the video: There is a way to implement this function and get to an alternative view of the RH. I decided to implement the Van Mangoldt function, using some simple techniques (Sieve, Binary Search, and so on). No GAI used. The interesting part is that this number in the chart below should have an upper bound of 1/2 (green light) when X goes to infinite. Hard to see in this small example (100k), but if the calculations are right in my code and the RH is true, it will continue as an upper bound. This is the famous 1/2 line in the RH. Code down below, cheers, ACC. using System; using System.CodeDom; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RH { ...

Standard Finite-State-Machine (FSM) Implementation - II

Image
There are multiple ways to solve this problem, but I find a state machine implementation very intuitive, even if we have to leave some of the state transitions empty (no-op). The state manages whether you're in "same char" mode or "different char". Code is down below, cheers, ACC. Find Mismatched Segments Between Two Strings - LeetCode You are given two strings s1 and s2 of the same length. A mismatched segment is a maximal contiguous range of indices where the characters of s1 and s2 differ at every index. Return a 2D array containing [sub1, sub2] for each mismatched segment, where sub1 and sub2 are the corresponding substrings from s1 and s2 , respectively. Return the segments in increasing order of their starting indices. If there are no mismatched segments, return an empty array. A substring is a contiguous non-empty sequence of characters within a string.   Example 1: Input: s1 = "earth", s2 = "empty" Output: [["ar...