1

I'm working on this algorithm exercise but I don't understand completely the formulation. Here is the exercise:

Given a string str and array of pairs that indicates which indices in the string can be swapped, return the lexicographically largest string that results from doing the allowed swaps. You can swap indices any number of times.

Example

For str = "abdc" and pairs = [[1, 4], [3, 4]], the output should be swapLexOrder(str, pairs) = "dbca".

By swapping the given indices, you get the strings: "cbda", "cbad", "dbac", "dbca". The lexicographically largest string in this list is "dbca".

Input/Output

[execution time limit] 4 seconds (js)

[input] string str

A string consisting only of lowercase English letters.

Guaranteed constraints: 1 ≤ str.length ≤ 104.

[input] array.array.integer pairs

An array containing pairs of indices that can be swapped in str (1-based). This means that for each pairs[i], you can swap elements in str that have the indices pairs[i][0] and pairs[i][1].

Guaranteed constraints: 0 ≤ pairs.length ≤ 5000, pairs[i].length = 2.

[output] string

My question is, why "abcd" is not a posible answer (just swapping index 3 and 4 on the original string "abdc")? The example says

By swapping the given indices, you get the strings: "cbda", "cbad", "dbac", "dbca". The lexicographically largest string in this list is "dbca"

I understand that even if "abcd" is a possible answer "dbca" is lexicographically largest so the answer is the same. But if I don't understand why "abcd" is not a possible answer I think I'm misunderstanding the task

1 Answer 1

2

You are reading the question correctly, and their description is broken. Both "abcd" and "abdc" are on the list of possible strings that you can produce, and yet are not in their list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.