2273. Find Resultant Array After Removing Anagrams
Easy
You are given a 0-indexed string array words
, where words[i]
consists of lowercase English letters.
In one operation, select any index i
such that 0 < i < words.length
and words[i - 1]
and words[i]
are anagrams, and delete words[i]
from words
. Keep performing this operation as long as you can select an index that satisfies the conditions.
Return words
after performing all operations. It can be shown that selecting the indices for each operation in any arbitrary order will lead to the same result.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase using all the original letters exactly once. For example, "dacb"
is an anagram of "abdc"
.
Example 1:
Example 2:
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 10
words[i]
consists of lowercase English letters.
解題
透過 map 紀錄目前出現的字元個數,之後的詞如果字元個數皆相同,不放進要回傳的答案內;不同的話清空map ,改紀錄新的字串出現的各個字元個數。
Runtime: 4 ms, faster than 81.08%
Memory Usage: 3.5 MB, less than 54.05%
Last updated