# Posts tagged as “easy”

Given an array of strings words and a string s, determine if s is an acronym of words.

The string s is considered an acronym of words if it can be formed by concatenating the first character of each string in words in order. For example, "ab" can be formed from ["apple", "banana"], but it can’t be formed from ["bear", "aardvark"].

Return true if s is an acronym of words, and false otherwise.

Example 1:

Input: words = ["alice","bob","charlie"], s = "abc"
Output: true
Explanation: The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym.


Example 2:

Input: words = ["an","apple"], s = "a"
Output: false
Explanation: The first character in the words "an" and "apple" are 'a' and 'a', respectively.
The acronym formed by concatenating these characters is "aa".
Hence, s = "a" is not the acronym.


Example 3:

Input: words = ["never","gonna","give","up","on","you"], s = "ngguoy"
Output: true
Explanation: By concatenating the first character of the words in the array, we get the string "ngguoy".
Hence, s = "ngguoy" is the acronym.


Constraints:

• 1 <= words.length <= 100
• 1 <= words[i].length <= 10
• 1 <= s.length <= 100
• words[i] and s consist of lowercase English letters.

## Solution: Check the first letter of each word

No need to concatenate, just check the first letter of each word.

Time complexity: O(n)
Space complexity: O(1)

## C++

Given a 0-indexed integer array nums of length n and an integer target, return the number of pairs(i, j)where0 <= i < j < nandnums[i] + nums[j] < target.

Example 1:

Input: nums = [-1,1,2,3,1], target = 2
Output: 3
Explanation: There are 3 pairs of indices that satisfy the conditions in the statement:
- (0, 1) since 0 < 1 and nums + nums = 0 < target
- (0, 2) since 0 < 2 and nums + nums = 1 < target
- (0, 4) since 0 < 4 and nums + nums = 0 < target
Note that (0, 3) is not counted since nums + nums is not strictly less than the target.


Example 2:

Input: nums = [-6,2,5,-2,-7,-1,3], target = -2
Output: 10
Explanation: There are 10 pairs of indices that satisfy the conditions in the statement:
- (0, 1) since 0 < 1 and nums + nums = -4 < target
- (0, 3) since 0 < 3 and nums + nums = -8 < target
- (0, 4) since 0 < 4 and nums + nums = -13 < target
- (0, 5) since 0 < 5 and nums + nums = -7 < target
- (0, 6) since 0 < 6 and nums + nums = -3 < target
- (1, 4) since 1 < 4 and nums + nums = -5 < target
- (3, 4) since 3 < 4 and nums + nums = -9 < target
- (3, 5) since 3 < 5 and nums + nums = -3 < target
- (4, 5) since 4 < 5 and nums + nums = -8 < target
- (4, 6) since 4 < 6 and nums + nums = -4 < target


Constraints:

• 1 <= nums.length == n <= 50
• -50 <= nums[i], target <= 50

## Solution 1: Brute force

Enumerate all pairs.

Time complexity: O(n2)
Space complexity: O(1)

## Solution 2: Two Pointers

Sort the numbers.

Use two pointers i, and j.
Set i to 0 and j to n – 1.
while (nums[i] + nums[j] >= target) –j
then we have nums[i] + nums[k] < target (i < k <= j), in total (j – i) pairs.
++i, move to the next starting number.
Time complexity: O(nlogn + n)
Space complexity: O(1)

## C++

You are given a 0-indexed integer array nums. You have to find the maximum sum of a pair of numbers from nums such that the maximum digit in both numbers are equal.

Return the maximum sum or -1 if no such pair exists.

Example 1:

Input: nums = [51,71,17,24,42]
Output: 88
Explanation:
For i = 1 and j = 2, nums[i] and nums[j] have equal maximum digits with a pair sum of 71 + 17 = 88.
For i = 3 and j = 4, nums[i] and nums[j] have equal maximum digits with a pair sum of 24 + 42 = 66.
It can be shown that there are no other pairs with equal maximum digits, so the answer is 88.

Example 2:

Input: nums = [1,2,3,4]
Output: -1
Explanation: No pair exists in nums with equal maximum digits.


Constraints:

• 2 <= nums.length <= 100
• 1 <= nums[i] <= 104

## Solution: Brute Force

Enumerate all pairs of nums and check their sum and max digit.

Time complexity: O(n2)
Space complexity: O(1)

## C++

You are given two integers, num and t.

An integer x is called achievable if it can become equal to num after applying the following operation no more than t times:

• Increase or decrease x by 1, and simultaneously increase or decrease num by 1.

Return the maximum possible achievable number. It can be proven that there exists at least one achievable number.

Example 1:

Input: num = 4, t = 1
Output: 6
Explanation: The maximum achievable number is x = 6; it can become equal to num after performing this operation:
1- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5.
It can be proven that there is no achievable number larger than 6.


Example 2:

Input: num = 3, t = 2
Output: 7
Explanation: The maximum achievable number is x = 7; after performing these operations, x will equal num:
1- Decrease x by 1, and increase num by 1. Now, x = 6 and num = 4.
2- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5.
It can be proven that there is no achievable number larger than 7.


Constraints:

• 1 <= num, t <= 50

## Solution: Greedy

Always decrease x and always increase num, the max achievable number x = num + t * 2

Time complexity: O(1)
Space complexity: O(1)

## C++

Write a function createHelloWorld. It should return a new function that always returns "Hello World".

Example 1:

Input: args = []
Output: "Hello World"
Explanation:
const f = createHelloWorld();
f(); // "Hello World"
The function returned by createHelloWorld should always return "Hello World".


Example 2:

Input: args = [{},null,42]
Output: "Hello World"
Explanation:
const f = createHelloWorld();
f({}, null, 42); // "Hello World"
Any arguments could be passed to the function but it should still always return "Hello World".


Constraints:

• 0 <= args.length <= 10

Solution: