[LeetCode] Find Substring With Given Hash Value

2156. Find Substring With Given Hash Value

The hash of a 0-indexed string s of length k, given integers p and m, is computed using the following function:

  • hash(s, p, m) = (val(s[0]) p0 + val(s[1]) p1 + … + val(s[k-1]) * pk-1) mod m.
    Where val(s[i]) represents the index of s[i] in the alphabet from val(‘a’) = 1 to val(‘z’) = 26.

You are given a string s and the integers power, modulo, k, and hashValue. Return sub, the first substring of s of length k such that hash(sub, power, modulo) == hashValue.

The test cases will be generated such that an answer always exists.

A substring is a contiguous non-empty sequence of characters within a string.

Read more
[Google foobar] Level 3 Bomb, Baby!

Google foobar challenge level 3 - Bomb, Baby!

Read more
[Google foobar] Level 3 Fuel Injection Perfection

Google foobar challenge level 3 - Fuel Injection Perfection

Read more
[Google foobar] Level 3 Doomsday Fuel

Google foobar challenge level 3 - Doomsday Fuel

Read more
[Google foobar] Level 2 Ion Flux Relabeling

Google foobar challenge level 2 - Ion Flux Relabeling

Read more
[Google foobar] Level 2 Gearing Up for Destruction

Google foobar challenge level 2 - Gearing Up for Destruction

Read more
[Google foobar] Level 1 Minion Work Assignments

Google foobar challenge level 1 - Minion Work Assignments

Read more
[LeetCode] 24 Game

679. 24 Game

You are given an integer array cards of length 4. You have four cards, each containing a number in the range [1, 9]. You should arrange the numbers on these cards in a mathematical expression using the operators [‘+’, ‘-‘, ‘*’, ‘/‘] and the parentheses ‘(‘ and ‘)’ to get the value 24.

You are restricted with the following rules:

  • The division operator ‘/‘ represents real division, not integer division.
  • For example, 4 / (1 - 2 / 3) = 4 / (1 / 3) = 12.
  • Every operation done is between two numbers. In particular, we cannot use ‘-‘ as a unary operator.
  • For example, if cards = [1, 1, 1, 1], the expression “-1 - 1 - 1 - 1” is not allowed.
  • You cannot concatenate numbers together
  • For example, if cards = [1, 2, 1, 2], the expression “12 + 12” is not valid.

Return true if you can get such expression that evaluates to 24, and false otherwise.

Read more
[LeetCode] Determine if Two Strings Are Close

1657. Determine if Two Strings Are Close

Two strings are considered close if you can attain one from the other using the following operations:

  • Operation 1: Swap any two existing characters.
  • For example, abcde -> aecdb
  • Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
  • For example, aacabb -> bbcbaa (all a’s turn into b’s, and all b’s turn into a’s)
    You can use the operations on either string as many times as necessary.

Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.

Read more
[LeetCode] The Time When the Network Becomes Idle

2039. The Time When the Network Becomes Idle

There is a network of n servers, labeled from 0 to n - 1. You are given a 2D integer array edges, where edges[i] = [ui, vi] indicates there is a message channel between servers ui and vi, and they can pass any number of messages to each other directly in one second. You are also given a 0-indexed integer array patience of length n.

All servers are connected, i.e., a message can be passed from one server to any other server(s) directly or indirectly through the message channels.

The server labeled 0 is the master server. The rest are data servers. Each data server needs to send its message to the master server for processing and wait for a reply. Messages move between servers optimally, so every message takes the least amount of time to arrive at the master server. The master server will process all newly arrived messages instantly and send a reply to the originating server via the reversed path the message had gone through.

At the beginning of second 0, each data server sends its message to be processed. Starting from second 1, at the beginning of every second, each data server will check if it has received a reply to the message it sent (including any newly arrived replies) from the master server:

  • If it has not, it will resend the message periodically. The data server i will resend the message every patience[i] second(s), i.e., the data server i will resend the message if patience[i] second(s) have elapsed since the last time the message was sent from this server.

  • Otherwise, no more resending will occur from this server.
    The network becomes idle when there are no messages passing between servers or arriving at servers.

Return the earliest second starting from which the network becomes idle.

Read more