instruction_seed
stringlengths
29
40.5k
source
stringclasses
6 values
translate this code from Scheme to Clojure: ```scheme (define (split-groups n dislikes) (define (create-adj-list n dislikes) (define adj-list (make-vector (+ n 1) '())) (define (add-edge u v) (vector-set! adj-list u (cons v (vector-ref adj-list u))) (vector-set! adj-list v (cons u (vector-ref adj-...
dolphin
Please explain the reasoning behind the following solution and provide code in Kotlin: You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them. We repeatedly make **duplicate removals** on `s` until we no ...
dolphin
Please explain the reasoning behind the following solution and provide code in Lua: You are given two integers `n` and `maxValue`, which are used to describe an **ideal** array. A **0-indexed** integer array `arr` of length `n` is considered **ideal** if the following conditions hold: * Every `arr[i]` is a value fr...
dolphin
Please focus on the functionality of this problem and provide code in ANSI C: You are given a **0-indexed** integer array `nums` of length `n`. You are initially standing at index `0`. You can jump from index `i` to index `j` where `i < j` if: * `nums[i] <= nums[j]` and `nums[k] < nums[i]` for all indexes `k` in the...
dolphin
Please explain the reasoning behind the following solution and provide code in Dart: Given a string `s`, return _the length of the longest_ _substring_ _that contains at most **two distinct characters**_. **Example 1:** **Input:** s = "eceba " **Output:** 3 **Explanation:** The substring is "ece " which its length ...
dolphin
Please write the following solution using Object Pascal: Your car starts at position `0` and speed `+1` on an infinite number line. Your car can go into negative positions. Your car drives automatically according to a sequence of instructions `'A'` (accelerate) and `'R'` (reverse): * When you get an instruction `'A'...
dolphin
port this code to Rust: ```lisp (defun num-of-ways (steps arr-len) (let* ((mod 1000000007) (dp (make-array (list 2 arr-len) :initial-element 0))) (setf (aref dp 0 0) 1) (loop for s from 1 to steps do (loop for p from 0 to (min s (- arr-len 1)) do (setf (aref dp (mod s 2) p) ...
dolphin
translate this code from Mojo to Kotlin: ```mojo sub findClosestElements($arr is list of int, $k is int, $x is int) is list of int { my $left = 0; my $right = $arr.len - $k; while $left < $right { my $mid = $left + ($right - $left) div 2; if $x - $arr[$mid] > $arr[$mid + $k] - $x { ...
dolphin
convert this code to c++: ```erlang can_win(CurrentState) -> can_win(CurrentState, 1, length(CurrentState) - 1). can_win(CurrentState, I, Len) when I >= Len -> false; can_win(CurrentState, I, Len) -> case [ok || X <- lists:seq(I, Len - 1), string:substr(CurrentState, X, 2) == "++"] of [] -> false; ...
dolphin
translate this code from Objective-C to Groovy: ```objective-c @interface Solution : NSObject - (NSInteger)numSpecial:(NSArray<NSArray<NSNumber *> *> *)mat; @end @implementation Solution - (NSInteger)numSpecial:(NSArray<NSArray<NSNumber *> *> *)mat { NSMutableArray<NSNumber *> *rowCount = [NSMutableArray array]...
dolphin
Please focus on the efficiency of this problem and provide code in Haskell: Given a string `s` and a dictionary of strings `wordDict`, add spaces in `s` to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in **any order**. **Note** that the same word in the dictionary...
dolphin
Please explain the reasoning behind the following solution and provide code in c++: You are given a directed graph with `n` nodes labeled from `0` to `n - 1`, where each node has **exactly one** outgoing edge. The graph is represented by a given **0-indexed** integer array `edges` of length `n`, where `edges[i]` indic...
dolphin
Please explain the reasoning behind the following solution and provide code in Elixir: You are given a string `s` of lowercase English letters and a 2D integer array `shifts` where `shifts[i] = [starti, endi, directioni]`. For every `i`, **shift** the characters in `s` from the index `starti` to the index `endi` (**inc...
dolphin
translate this code from Mojo to javascript: ```mojo sub min_operations_to_satisfy_conditions($a, $b) { my $same = 0; my $less = 0; my $greater = 0; for my $i (0..(length($a)-1)) { if (substr($a, $i, 1) eq substr($b, $i, 1)) { $same++; } elsif (substr($a, $i, 1) lt s...
dolphin
Please explain the reasoning behind the following solution and provide code in R: You are given an integer array `matchsticks` where `matchsticks[i]` is the length of the `ith` matchstick. You want to use **all the matchsticks** to make one square. You **should not break** any stick, but you can link them up, and each ...
dolphin
translate this code from Erlang to Scala: ```erlang minimum_clips(Clips, Time) -> MaxCover = 0, End = 0, SortedClips = lists:sort(fun([Start1, _], [Start2, _]) -> Start1 < Start2 end, Clips), MinimumClips(SortedClips, End, Time, 0). MinimumClips([], End, Time, Count) when End >= Time -> Count; MinimumC...
dolphin
translate this code from R to Lua: ```R countComponents <- function(head, nums) { hashMap <- vector(mode = "list", length = 1000001) for (num in nums) { hashMap[[num+1]] <- TRUE } count <- 0 curr <- head while (!is.null(curr)) { if (!is.null(hashMap[[curr$val+1]]) && ...
dolphin
port this code from javascript to Haskell: ```javascript class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } class FindElements { constructor(root) { this.values = new Set(); this.recoverTree(root, ...
dolphin
Please explain the following problem in detail and provide code in Objective-C: You are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`. After placing these cubes, you have decided to glue any directly adja...
dolphin
Please focus on the efficiency of this problem and provide code in Go: You are given a **0-indexed** 2D integer array `peaks` where `peaks[i] = [xi, yi]` states that mountain `i` has a peak at coordinates `(xi, yi)`. A mountain can be described as a right-angled isosceles triangle, with its base along the `x`\-axis and...
dolphin
Please focus on the efficiency of this problem and provide code in Rust: Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_. Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclus...
dolphin
Please explain the following problem in detail and provide code in Objective-C: A perfectly straight street is represented by a number line. The street has building(s) on it and is represented by a 2D integer array `buildings`, where `buildings[i] = [starti, endi, heighti]`. This means that there is a building with `he...
dolphin
Please focus on the algorithm of this problem and provide code in c++: Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 ...
dolphin
port this code to Fortran: ```vb Imports System.Collections.Generic Public Module DNASequences Public Function FindRepeatedDnaSequences(s As String) As List(Of String) Dim dnaCounter As New Dictionary(Of String, Integer) Dim result As New List(Of String) For i As Integer = 0 To s.Length - ...
dolphin
Please explain the following problem in detail and provide code in java: Given an array of integers `nums` and an integer `k`, return _the number of **unique** k-diff pairs in the array_. A **k-diff** pair is an integer pair `(nums[i], nums[j])`, where the following are true: * `0 <= i, j < nums.length` * `i != j...
dolphin
Please write the following solution using Swift: You are given a **0-indexed** integer array `nums` of size `n` and a positive integer `k`. We call an index `i` in the range `k <= i < n - k` **good** if the following conditions are satisfied: * The `k` elements that are just **before** the index `i` are in **non-in...
dolphin
Please focus on the efficiency of this problem and provide code in Scheme: Given a list of folders `folder`, return _the folders after removing all **sub-folders** in those folders_. You may return the answer in **any order**. If a `folder[i]` is located within another `folder[j]`, it is called a **sub-folder** of it....
dolphin
Please explain the following problem in detail and provide code in Erlang: There is a country of `n` cities numbered from `0` to `n - 1`. In this country, there is a road connecting **every pair** of cities. There are `m` friends numbered from `0` to `m - 1` who are traveling through the country. Each one of them will...
dolphin
Please focus on the efficiency of this problem and provide code in Go: You are given a 2D integer array `intervals` where `intervals[i] = [lefti, righti]` represents the **inclusive** interval `[lefti, righti]`. You have to divide the intervals into one or more **groups** such that each interval is in **exactly** one ...
dolphin
Please focus on the functionality of this problem and provide code in Haskell: You are given a **0-indexed** array of strings `nums`, where each string is of **equal length** and consists of only digits. You are also given a **0-indexed** 2D integer array `queries` where `queries[i] = [ki, trimi]`. For each `queries[i...
dolphin
Please explain the following problem in detail and provide code in Elixir: Given an integer `n`, add a dot ( ". ") as the thousands separator and return it in string format. **Example 1:** **Input:** n = 987 **Output:** "987 " **Example 2:** **Input:** n = 1234 **Output:** "1.234 " **Constraints:** * `0 <= n ...
dolphin
Please explain the reasoning behind the following solution and provide code in Dart: Given a `root` of an [N-ary tree](https://leetcode.com/articles/introduction-to-n-ary-trees/), you need to compute the length of the diameter of the tree. The diameter of an N-ary tree is the length of the **longest** path between any...
dolphin
port this code from ANSI C to Object Pascal: ```c #define MOD 1000000007 int num_tilings(int n){ if(n == 1){ return 1; } int dp[n+1]; dp[1] = 1; dp[2] = 2; for(int i=3; i<=n; i++){ dp[i] = ((dp[i-1] + dp[i-2]) % MOD + (2 * dp[i-3]) % MOD) % MOD; } return d...
dolphin
Please explain the reasoning behind the following solution and provide code in c++: You are playing a video game where you are defending your city from a group of `n` monsters. You are given a **0-indexed** integer array `dist` of size `n`, where `dist[i]` is the **initial distance** in kilometers of the `ith` monster ...
dolphin
Please write the following solution using C#: In a special ranking system, each voter gives a rank from highest to lowest to all teams participating in the competition. The ordering of teams is decided by who received the most position-one votes. If two or more teams tie in the first position, we consider the second p...
dolphin
Please focus on the algorithm of this problem and provide code in Lisp: Two strings `X` and `Y` are similar if we can swap two letters (in different positions) of `X`, so that it equals `Y`. Also two strings `X` and `Y` are similar if they are equal. For example, `"tars "` and `"rats "` are similar (swapping at positi...
dolphin
Please focus on the efficiency of this problem and provide code in Erlang: Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses. Every house can be warmed, as long as the house is within the heater's warm radius range. Given the positions ...
dolphin
Please write the following solution using Mojo: You are given a **0-indexed** binary string `s` and two integers `minJump` and `maxJump`. In the beginning, you are standing at index `0`, which is equal to `'0'`. You can move from index `i` to index `j` if the following conditions are fulfilled: * `i + minJump <= j <...
dolphin
Please explain the reasoning behind the following solution and provide code in R: A binary tree is named **Even-Odd** if it meets the following conditions: * The root of the binary tree is at level index `0`, its children are at level index `1`, their children are at level index `2`, etc. * For every **even-indexe...
dolphin
Please explain the following problem in detail and provide code in Julia: A perfectly straight street is represented by a number line. The street has street lamp(s) on it and is represented by a 2D integer array `lights`. Each `lights[i] = [positioni, rangei]` indicates that there is a street lamp at position `position...
dolphin
Please explain the reasoning behind the following solution and provide code in Haskell: A sequence of numbers is called **arithmetic** if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence `s` is arithmetic if and only if `s[i+1] - s[i]...
dolphin
port this code to Go: ```elixir defmodule Solution do def max_fruits(fruits, start_pos, k) do left = start_pos right = start_pos max_fruits = 0 fruits_dict = %{} curr_fruits = 0 for {pos, amt} <- fruits do while pos > right + k do curr_fruits = curr_fruits - fruits_dict[right] ...
dolphin
Please explain the following problem in detail and provide code in Object Pascal: The [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) between two integers is the number of positions at which the corresponding bits are different. Given an integer array `nums`, return _the sum of **Hamming distances**...
dolphin
port this code to TypeScript: ``` sub get_folder_names($names) { my %name_count; my @ans; for my $name (@$names) { if (not exists $name_count{$name}) { push @ans, $name; $name_count{$name} = 1; } else { my $k = $name_count{$name}; my $...
dolphin
translate this code to javascript: ```dart import 'package:sorted_set/sorted_set.dart'; bool constructArray(List<int> target) { int n = target.length; int totalSum = target.reduce((a, b) => a + b); if (totalSum % n != 0) { return false; } int x = totalSum ~/ n; List<int> arr = List.filled(n, 1); for ...
dolphin
convert this code from javascript to Fortran: ```javascript function findCheapestPrice(n, flights, src, dst, k) { const graph = Array.from({ length: n }, () => []); for (const flight of flights) { graph[flight[0]].push([flight[1], flight[2]]); } const pq = [[0, src, k + 1]]; while (pq.lengt...
dolphin
convert this code to Swift: ```fsharp type ListNode(val : int, next : ListNode option) = member this.Val = val member this.Next = next let rec deleteDuplicates (head : ListNode option) = match head with | Some current -> if current.Next.IsSome && current.Val = current.Next.Value.Val then ...
dolphin
Please explain the following problem in detail and provide code in Dart: You're given strings `jewels` representing the types of stones that are jewels, and `stones` representing the stones you have. Each character in `stones` is a type of stone you have. You want to know how many of the stones you have are also jewels...
dolphin
Please explain the following problem in detail and provide code in Fortran: You and a gang of thieves are planning on robbing a bank. You are given a **0-indexed** integer array `security`, where `security[i]` is the number of guards on duty on the `ith` day. The days are numbered starting from `0`. You are also given ...
dolphin
Please explain the following problem in detail and provide code in F#: Design a queue-like data structure that moves the most recently used element to the end of the queue. Implement the `MRUQueue` class: * `MRUQueue(int n)` constructs the `MRUQueue` with `n` elements: `[1,2,3,...,n]`. * `int fetch(int k)` moves ...
dolphin
port this code to Objective-C: ```typescript function findTheDistanceValue(arr1: number[], arr2: number[], d: number): number { let count: number = 0; for (let i: number = 0; i < arr1.length; i++) { let valid: boolean = true; for (let j: number = 0; j < arr2.length; j++) { if (Math.a...
dolphin
convert this code from Clojure to c++: ```clojure (defn can-rearrange-to-arithmetic-progression [arr] (let [arr-length (count arr)] (if (< arr-length 2) true (let [sorted-arr (sort arr) difference (- (nth sorted-arr 1) (nth sorted-arr 0))] (loop [i 2] (if (< i arr-length)...
dolphin
Please explain the following problem in detail and provide code in Rust: Given an array of integers `arr` and an integer `k`. Find the _least number of unique integers_ after removing **exactly** `k` elements**.** **Example 1:** **Input:** arr = \[5,5,4\], k = 1 **Output:** 1 **Explanation**: Remove the single 4, onl...
dolphin
Please write the following solution using Elixir: Given the `root` of a binary tree and an integer `targetSum`, return _the number of paths where the sum of the values along the path equals_ `targetSum`. The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from p...
dolphin
port this code from Groovy to PHP: ``` def countPossibleRoutes(locations, start, finish, fuel) { def n = locations.size() def dp = [new int[fuel+1] as int]*n for (int i = 0; i <= fuel; i++) { if (Math.abs(locations[start] - locations[finish]) <= i) { dp[start][i] = 1 } } ...
dolphin
Please explain the reasoning behind the following solution and provide code in Dart: Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return _the only number in the range that is missing from the array._ **Example 1:** **Input:** nums = \[3,0,1\] **Output:** 2 **Explanation:** n = 3 since ...
dolphin
Please explain the reasoning behind the following solution and provide code in Ruby: You are given an array `nums` consisting of **positive** integers. We call a subarray of `nums` **nice** if the bitwise **AND** of every pair of elements that are in **different** positions in the subarray is equal to `0`. Return _th...
dolphin
convert this code to Perl: ```go type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func delNodes(root *TreeNode, to_delete []int) []*TreeNode { to_delete_set := make(map[int]bool) for _, val := range to_delete { to_delete_set[val] = true } remaining_trees := []*TreeNod...
dolphin
Please explain the reasoning behind the following solution and provide code in TypeScript: You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two j...
dolphin
Please focus on the functionality of this problem and provide code in Elixir: Given an integer array `nums` of length `n` and an integer `k`, return _the_ `kth` _**smallest subarray sum**._ A **subarray** is defined as a **non-empty** contiguous sequence of elements in an array. A **subarray sum** is the sum of all el...
dolphin
Please write the following solution using Lua: You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used....
dolphin
Please explain the reasoning behind the following solution and provide code in R: You are given a 2D integer array `items` where `items[i] = [pricei, weighti]` denotes the price and weight of the `ith` item, respectively. You are also given a **positive** integer `capacity`. Each item can be divided into two items wi...
dolphin
Please focus on the efficiency of this problem and provide code in c++: Given a `rows x cols` binary `matrix` filled with `0`'s and `1`'s, find the largest rectangle containing only `1`'s and return _its area_. **Example 1:** **Input:** matrix = \[\[ "1 ", "0 ", "1 ", "0 ", "0 "\],\[ "1 ", "0 ", "1 ", "1 ", "1 "\],\[...
dolphin
Please focus on the efficiency of this problem and provide code in Clojure: There is an authentication system that works with authentication tokens. For each session, the user will receive a new authentication token that will expire `timeToLive` seconds after the `currentTime`. If the token is renewed, the expiry time ...
dolphin
Please write the following solution using Go: A binary tree is **uni-valued** if every node in the tree has the same value. Given the `root` of a binary tree, return `true` _if the given tree is **uni-valued**, or_ `false` _otherwise._ **Example 1:** **Input:** root = \[1,1,1,1,1,null,1\] **Output:** true **Example...
dolphin
convert this code from Fortran to Scheme: ```fortran function searchMatrix(matrix, target) result(found) integer, intent(in) :: matrix(:,:) integer, intent(in) :: target logical :: found integer :: m, n, l, r, mid, mid_val m = size(matrix, 1) n = size(matrix, 2) l = 1 r = m * n ...
dolphin
Please explain the following problem in detail and provide code in TypeScript: Given the availability time slots arrays `slots1` and `slots2` of two people and a meeting duration `duration`, return the **earliest time slot** that works for both of them and is of duration `duration`. If there is no common time slot tha...
dolphin
Please explain the reasoning behind the following solution and provide code in Go: Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_. Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element. You must solve it in `O(n)` time comple...
dolphin
Please write the following solution using ANSI C: Given a string `s` and an array of strings `words`, return _the number of_ `words[i]` _that is a subsequence of_ `s`. A **subsequence** of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relati...
dolphin
Please focus on the functionality of this problem and provide code in Erlang: You are given a **0-indexed** 2D integer array `questions` where `questions[i] = [pointsi, brainpoweri]`. The array describes the questions of an exam, where you have to process the questions **in order** (i.e., starting from question `0`) a...
dolphin
Please write the following solution using Visual Basic .NET: There is a function `signFunc(x)` that returns: * `1` if `x` is positive. * `-1` if `x` is negative. * `0` if `x` is equal to `0`. You are given an integer array `nums`. Let `product` be the product of all values in the array `nums`. Return `signFunc...
dolphin
Please focus on the algorithm of this problem and provide code in Julia: On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return _the **minimum time** in seconds to visit all the points in the order given by_ `points`. You can move according to these rules: * In `1` second, you c...
dolphin
Please write the following solution using Objective-C: Given an integer array `nums` and two integers `k` and `p`, return _the number of **distinct subarrays** which have **at most**_ `k` _elements divisible by_ `p`. Two arrays `nums1` and `nums2` are said to be **distinct** if: * They are of **different** lengths,...
dolphin
Please write the following solution using Perl: Given a string `s` and an integer `k`, return _the length of the longest_ _substring_ _of_ `s` _that contains at most_ `k` _**distinct** characters_. **Example 1:** **Input:** s = "eceba ", k = 2 **Output:** 3 **Explanation:** The substring is "ece " with length 3. *...
dolphin
Please explain the reasoning behind the following solution and provide code in Visual Basic .NET: Given a string `n` representing an integer, return _the closest integer (not including itself), which is a palindrome_. If there is a tie, return _**the smaller one**_. The closest is defined as the absolute difference mi...
dolphin
Please explain the reasoning behind the following solution and provide code in Go: Given the array `restaurants` where `restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]`. You have to filter the restaurants using three filters. The `veganFriendly` filter will be either _true_ (meaning you should only ...
dolphin
Please write the following solution using Lua: Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `...
dolphin
Please explain the following problem in detail and provide code in Julia: Sometimes people repeat letters to represent extra feeling. For example: * `"hello " -> "heeellooo "` * `"hi " -> "hiiii "` In these strings like `"heeellooo "`, we have groups of adjacent letters that are all the same: `"h "`, `"eee "`, `"...
dolphin
Please focus on the functionality of this problem and provide code in Mojo: You are given an array of integers `stones` where `stones[i]` is the weight of the `ith` stone. We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones...
dolphin
Please explain the following problem in detail and provide code in Elixir: You are given `n` **BST (binary search tree) root nodes** for `n` separate BSTs stored in an array `trees` (**0-indexed**). Each BST in `trees` has **at most 3 nodes**, and no two roots have the same value. In one operation, you can: * Select...
dolphin
convert this code from F# to PHP: ```fsharp let maxSumUnderK (nums: int array) (k: int) = let mutable maxSum = -1 for i = 0 to nums.Length - 2 do for j = i + 1 to nums.Length - 1 do let sum = nums.[i] + nums.[j] if sum < k && sum > maxSum then maxSum <- sum if...
dolphin
Please write the following solution using R: Given a rectangle of size `n` x `m`, return _the minimum number of integer-sided squares that tile the rectangle_. **Example 1:** **Input:** n = 2, m = 3 **Output:** 3 **Explanation:** `3` squares are necessary to cover the rectangle. `2` (squares of `1x1`) `1` (square of ...
dolphin
convert this code to Kotlin: ```erlang k_palindrome(S, K) -> case is_palindrome(S) of true -> true; false -> case K =< 0 of true -> false; false -> I = 1, J = length(S), k_palindrome(S, K, I, J) ...
dolphin
Please focus on the functionality of this problem and provide code in TypeScript: You are given `coordinates`, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference. Return `true` _if the square is white, and_ `false` _if the square is black_. The coordinate ...
dolphin
translate this code from F# to Haskell: ```fsharp let makeConnected (n:int) (connections:int[][]) = let adjList = Array.zeroCreate n |> Array.map (fun _ -> List.empty<int>) let visited = Array.zeroCreate n |> Array.map (fun _ -> false) let mutable components = 0 let mutable extraEdges = 0 connectio...
dolphin
Please focus on the algorithm of this problem and provide code in R: A **sentence** is a string of single-space separated words where each word consists only of lowercase letters. A word is **uncommon** if it appears exactly once in one of the sentences, and **does not appear** in the other sentence. Given two **sent...
dolphin
Please focus on the efficiency of this problem and provide code in python: You are given a binary array `nums` and an integer `k`. A **k-bit flip** is choosing a **subarray** of length `k` from `nums` and simultaneously changing every `0` in the subarray to `1`, and every `1` in the subarray to `0`. Return _the minim...
dolphin
translate this code from Objective-C to Rust: ```objective-c // Definition for a binary tree node. @interface TreeNode : NSObject @property(nonatomic,assign) NSInteger val; @property(nonatomic,strong) TreeNode *left; @property(nonatomic,strong) TreeNode *right; @end @implementation TreeNode @end NSInteger sumPaths(Tr...
dolphin
Please focus on the efficiency of this problem and provide code in c++: You have an `inventory` of different colored balls, and there is a customer that wants `orders` balls of **any** color. The customer weirdly values the colored balls. Each colored ball's value is the number of balls **of that color** you currently...
dolphin
Please write the following solution using Perl: You are given an array of unique strings `words` where `words[i]` is six letters long. One word of `words` was chosen as a secret word. You are also given the helper object `Master`. You may call `Master.guess(word)` where `word` is a six-letter-long string, and it must ...
dolphin
translate this code from Clojure to Elixir: ``` (defn sufficient-subset [root limit] (if (nil? root) nil (if (and (nil? (:left root)) (nil? (:right root))) (if (< (:val root) limit) nil root) (do (swap! root (fn [node] (assoc node :left (sufficient-sub...
dolphin
Please focus on the efficiency of this problem and provide code in Erlang: Given a string s, return _all the palindromic permutations (without duplicates) of it_. You may return the answer in **any order**. If `s` has no palindromic permutation, return an empty list. **Example 1:** **Input:** s = "aabb" **Output:** ...
dolphin
translate this code from Objective-C to Mojo: ```objective-c NSArray* fizzBuzz(int n) { NSMutableArray* result = [[NSMutableArray alloc] initWithCapacity:n]; for (int i = 1; i <= n; i++) { if (i % 15 == 0) { [result addObject:@"FizzBuzz"]; } else if (i % 3 == 0) { [result...
dolphin
Please focus on the algorithm of this problem and provide code in C#: There is an `8 x 8` chessboard containing `n` pieces (rooks, queens, or bishops). You are given a string array `pieces` of length `n`, where `pieces[i]` describes the type (rook, queen, or bishop) of the `ith` piece. In addition, you are given a 2D i...
dolphin
Please write the following solution using TypeScript: Given `head` which is a reference node to a singly-linked list. The value of each node in the linked list is either `0` or `1`. The linked list holds the binary representation of a number. Return the _decimal value_ of the number in the linked list. The **most sig...
dolphin
Please write the following solution using F#: You are given two binary trees `root1` and `root2`. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes ov...
dolphin
Please focus on the efficiency of this problem and provide code in Elixir: Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,...
dolphin
Please write the following solution using Clojure: Given a `m x n` matrix `mat` and an integer `k`, return _a matrix_ `answer` _where each_ `answer[i][j]` _is the sum of all elements_ `mat[r][c]` _for_: * `i - k <= r <= i + k,` * `j - k <= c <= j + k`, and * `(r, c)` is a valid position in the matrix. **Example...
dolphin
Please write the following solution using Clojure: You are given an array of positive integers `price` where `price[i]` denotes the price of the `ith` candy and a positive integer `k`. The store sells baskets of `k` **distinct** candies. The **tastiness** of a candy basket is the smallest absolute difference of the **...
dolphin
Please explain the following problem in detail and provide code in Scala: Given an `m x n` matrix `grid` where each cell is either a wall `'W'`, an enemy `'E'` or empty `'0'`, return _the maximum enemies you can kill using one bomb_. You can only place the bomb in an empty cell. The bomb kills all the enemies in the s...
dolphin