java.lang.Object
g2201_2300.s2273_find_resultant_array_after_removing_anagrams.Solution

public class Solution extends Object
2273 - Find Resultant Array After Removing Anagrams.<p>Easy</p> <p>You are given a <strong>0-indexed</strong> string array <code>words</code>, where <code>words[i]</code> consists of lowercase English letters.</p> <p>In one operation, select any index <code>i</code> such that <code>0 < i < words.length</code> and <code>words[i - 1]</code> and <code>words[i]</code> are <strong>anagrams</strong> , and <strong>delete</strong> <code>words[i]</code> from <code>words</code>. Keep performing this operation as long as you can select an index that satisfies the conditions.</p> <p>Return <code>words</code> <em>after performing all operations</em>. It can be shown that selecting the indices for each operation in <strong>any</strong> arbitrary order will lead to the same result.</p> <p>An <strong>Anagram</strong> 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, <code>&quot;dacb&quot;</code> is an anagram of <code>&quot;abdc&quot;</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;abba&rdquo;,&ldquo;baba&rdquo;,&ldquo;bbaa&rdquo;,&ldquo;cd&rdquo;,&ldquo;cd&rdquo;]</p> <p><strong>Output:</strong> [&ldquo;abba&rdquo;,&ldquo;cd&rdquo;]</p> <p><strong>Explanation:</strong> One of the ways we can obtain the resultant array is by using the following operations:</p> <ul> <li> <p>Since words[2] = &ldquo;bbaa&rdquo; and words[1] = &ldquo;baba&rdquo; are anagrams, we choose index 2 and delete words[2]. Now words = [&ldquo;abba&rdquo;,&ldquo;baba&rdquo;,&ldquo;cd&rdquo;,&ldquo;cd&rdquo;].</p> </li> <li> <p>Since words[1] = &ldquo;baba&rdquo; and words[0] = &ldquo;abba&rdquo; are anagrams, we choose index 1 and delete words[1]. Now words = [&ldquo;abba&rdquo;,&ldquo;cd&rdquo;,&ldquo;cd&rdquo;].</p> </li> <li> <p>Since words[2] = &ldquo;cd&rdquo; and words[1] = &ldquo;cd&rdquo; are anagrams, we choose index 2 and delete words[2]. Now words = [&ldquo;abba&rdquo;,&ldquo;cd&rdquo;].</p> </li> </ul> <p>We can no longer perform any operations, so [&ldquo;abba&rdquo;,&ldquo;cd&rdquo;] is the final answer.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;a&rdquo;,&ldquo;b&rdquo;,&ldquo;c&rdquo;,&ldquo;d&rdquo;,&ldquo;e&rdquo;]</p> <p><strong>Output:</strong> [&ldquo;a&rdquo;,&ldquo;b&rdquo;,&ldquo;c&rdquo;,&ldquo;d&rdquo;,&ldquo;e&rdquo;]</p> <p><strong>Explanation:</strong> No two adjacent strings in words are anagrams of each other, so no operations are performed.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= words.length <= 100</code></li> <li><code>1 <= words[i].length <= 10</code></li> <li><code>words[i]</code> consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details