返回介绍

solution / 2200-2299 / 2273.Find Resultant Array After Removing Anagrams / README_EN

发布于 2024-06-17 01:03:08 字数 4402 浏览 0 评论 0 收藏 0

2273. Find Resultant Array After Removing Anagrams

中文文档

Description

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:

Input: words = ["abba","baba","bbaa","cd","cd"]
Output: ["abba","cd"]
Explanation:
One of the ways we can obtain the resultant array is by using the following operations:
- Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2].
  Now words = ["abba","baba","cd","cd"].
- Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1].
  Now words = ["abba","cd","cd"].
- Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
  Now words = ["abba","cd"].
We can no longer perform any operations, so ["abba","cd"] is the final answer.

Example 2:

Input: words = ["a","b","c","d","e"]
Output: ["a","b","c","d","e"]
Explanation:
No two adjacent strings in words are anagrams of each other, so no operations are performed.

 

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 10
  • words[i] consists of lowercase English letters.

Solutions

Solution 1

class Solution:
  def removeAnagrams(self, words: List[str]) -> List[str]:
    return [
      w
      for i, w in enumerate(words)
      if i == 0 or sorted(w) != sorted(words[i - 1])
    ]
class Solution {
  public List<String> removeAnagrams(String[] words) {
    List<String> ans = new ArrayList<>();
    String prev = "";
    for (String w : words) {
      char[] cs = w.toCharArray();
      Arrays.sort(cs);
      String t = String.valueOf(cs);
      if (!t.equals(prev)) {
        ans.add(w);
      }
      prev = t;
    }
    return ans;
  }
}
function removeAnagrams(words: string[]): string[] {
  const n = words.length;
  let ans = [];
  ans.push(words[0]);
  let pre = countWord(words[0]).join('');
  for (let i = 1; i < n; i++) {
    let cur = countWord(words[i]).join('');
    if (pre !== cur) {
      ans.push(words[i]);
      pre = cur;
    }
  }
  return ans;
}

function countWord(word: string): number[] {
  let count = new Array(128).fill(0);
  for (let i = 0; i < word.length; i++) {
    count[word.charCodeAt(i)]++;
  }
  return count;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文