You keep a shopping list, an ArrayBuffer of strings items. Add the word first to the end of the list, then add the word second to the end, then undo the last add by removing the last item. Return the list.
The undo always removes the item at the end of the list, even when the same word also appears earlier in the list.
Example 1:
Input: items = ["milk", "eggs"], first = "bread", second = "jam" Output: ["milk", "eggs", "bread"] Explanation: After the two adds the list is ["milk", "eggs", "bread", "jam"]. The undo removes "jam", the last item.
Example 2:
Input: items = [], first = "apples", second = "pears" Output: ["apples"] Explanation: The list starts empty. "apples" and "pears" are added, then "pears" is removed.
Example 3:
Input: items = ["water"], first = "juice", second = "water" Output: ["water", "juice"] Explanation: The list becomes ["water", "juice", "water"]. The undo removes the last item, the "water" just added, not the first one.
Constraints:
itemshas between 0 and 10 words: it may be empty.first,secondand every word initemsare single words of lowercase English letters.- The same word may appear more than once.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
