An English lecture at HackerElementary School is aimed at teaching students the letters of the alphabet. Lang Au The students are provided with a string word that consists of lowercase English letters. In one move, they can choose any index i, and let the character at that index be c. Then, the first occurrence of c to the left of i, and the first occurrence of c to the right of iare deleted (Note: the operation can still be carried out even if either the left or right occurrence doesn't exist). For example, if word = "adabacaea", and if index 4 is chosen (0-based), the first occurrence of 'a' to the left and right of index 4 (bold, indices 2 and 6) are deleted leaving word = "adbacea". ALL 1 15 16 17 18 19 20 21 Find the minimum number of moves the students need to perform in order to get a word of minimal length. 22 23 24 25 1 Example 2 Consider word = "baabacaa". The following moves are optimal. 1. Choose index 0, "baabacaa", then word = "baaacaa". Delete the b to its right at index 3. There is no b to its left so the operation is finished. 2. Now, choose index 2, "baaacaa", then word = "bacaa". 3. Now, choose index 3, "bacaa", then word = "bca". The word cannot be reduced further. The answer is 3. Function Description Complete the function getMinimum Moves in the editor below. getMinimum Moves has the following parameter: string word: the word given to the students. Returns 1>

Respuesta :

Answer:

Hi.

Explanation:

Certainly! Let's break down the problem and find the minimum number of moves the students need to perform in order to get a word of minimal length.

Given the string `word` consisting of lowercase English letters, the students can perform the following operation:

1. Choose any index `i`.

2. Let the character at that index be `c`.

3. Delete the first occurrence of `c` to the left of index `i`, and the first occurrence of `c` to the right of index `i` (even if either the left or right occurrence doesn't exist).

For example, consider the word `"adabacaea"`. If we choose index 4 (0-based), the first occurrence of `'a'` to the left (at index 2) and the first occurrence of `'a'` to the right (at index 6) are deleted, resulting in the word `"adbacea"`.

To find the minimum number of moves, we need to iteratively choose indices and perform the deletion operation until the word cannot be reduced further.

Let's illustrate this with the given example:

1. **Choose index 0**: `"baabacaa"`. Delete the `'b'` to its right at index 3. There is no `'b'` to its left, so the operation is finished. Word becomes `"baaacaa"`.

2. **Choose index 2**: `"baaacaa"`. Delete the `'a'` to its right at index 3. Word becomes `"bacaa"`.

3. **Choose index 3**: `"bacaa"`. Delete the `'c'` to its left at index 1. Word becomes `"bca"`.

The word cannot be reduced further, and the answer is **3**.

Now, let's implement the `getMinimumMoves` function to find the minimum moves for any given word.

Q&A Education