# LeetCode Weekly Contest 216 Swift Solutions

## Surprisingly Easy

This article is about the 4 challenges in the LeetCode Weekly Contest 216. That is

- 1662 Check If Two String Arrays are Equivalent
- 1663 Smallest String With A Given Numeric Value
- 1664 Ways to Make a Fair Array
- 1665 Minimum Initial Energy to Finish Tasks The solutions assume some knowledge of Big O notation

## The Problems

Each problem will be approached in turn, with a solution and also with articles explaining the tools, techniques and theory that will help you solve these problems yourself.

Let us get started!

## 1662 Check If Two String Arrays are Equivalent

This task is worth three points — so we would not expect this to be too difficult.

The question asks us it two arrays of `String`

are equivalent when the constituent element of the `String`

array are concatenated in order.

That is

In Swift, we are lucky in that we have a joined `func joined(separator: String = "") -> String`

that gives us exactly the solution we are looking for.

## 1663 Smallest String With A Given Numeric Value

Assume we can take the alphabet and assigning a 1-indexed value to each, therefore a = 1 and c = 3.

We are given the task of returning the smallest String with a length of `n`

, and a numeric value of `k`

(using the rubric above).

This means that, effectively we want to add the largest possible character to the back of the answer String.

Taking a quick look through by own article about Strings means we have a way of converting `String(Character(UnicodeScalar(26 + 96)!))`

which of course makes sense since in ASCII charts reveal `95 = a`

.

## 1664 Ways to Make a Fair Array

Given an array of Integers, where one element is removed from the array how many of arrays meet the condition evenElements == oddElements.

Rather than removing the elements in turn, and then calculating whether the odd numbers equal the even numers we can use the previous result to calculate the next one.

We can use `reduce`

to calculate the initial odd and even number arrays.

So if the element we wish to “remove” (although we are not actually removing anything), we remove the number from the relevant array:

we calculate whether the array meets the condition

and then return the element — but into position for the next iteration (so try to remove the next array element).

Here is the complete solution:

## 1665 Minimum Initial Energy to Finish Tasks

Given an array, each contains [a,b] where a = the energy required to finish a task and b = the energy required to start a task.

The LeetCode problem is to calculate the minimum amount of energy required to finish all of the tasks.

To do so, we sort the array into decreasing order of the difference between the energy required to finish the task and the the energy required to start the task.

we then work to calculate the required energy. For each task, If we don’t enough energy for the current task we log the amount of energy that is required (over and above the current amount). We then caculate the current energy required, and of course return the amont of required energy.

If you’ve any questions, comments or suggestions please hit me up on Twitter