PengWu3 min read

LeetCode2273

Solution

class Solution {
    public List<String> removeAnagrams(String[] words) {
        // Convert the array into a list for easier element removal.
        List<String> res = new LinkedList<>(Arrays.asList(words));

        int index = 1;
        // Iterate through the list and check consecutive elements.
        // Continue as long as the list is not empty and the index is within range.
        while (!res.isEmpty() && index < res.size()) {
            // Check if the current word and the previous one are anagrams.
            if (isAnagram(res.get(index - 1), res.get(index))) {
                res.remove(index); // Remove the latter one if they are anagrams.
            } else {
                index++; // Move to the next element otherwise.
            }
        }
        return res;
    }

    private boolean isAnagram(String word1, String word2) {
        int[] arr = new int[26]; // Use an array to count character frequencies.

        // First, check if the lengths are different — if so, they can’t be anagrams.
        if (word1.length() != word2.length()) return false;

        // Count the frequency of each letter in word1.
        for (char c : word1.toCharArray()) {
            arr[c - 'a']++;
        }

        // Decrease the count using letters in word2.
        for (char c : word2.toCharArray()) {
            arr[c - 'a']--;
        }

        // If all counts are zero, they are anagrams.
        for (int count : arr) {
            if (count != 0) return false;
        }

        return true;
    }
}

What to Say in an Interview

Problem Understanding

The problem asks us to remove consecutive words in the list if they are anagrams of each other. We repeat this process until no consecutive pair of anagrams remains.


Approach Overview

“I used a two-pointer style iteration to remove consecutive anagrams from the list.”

Step-by-step logic:

  1. Convert the input array into a List, because lists are easier to modify (remove elements).
  2. Iterate through the list, comparing each pair of consecutive words.
  3. If two consecutive words are anagrams, remove the second one.
  4. Otherwise, move to the next index.
  5. Continue this process until reaching the end of the list.

How to Check Anagrams

To check whether two words are anagrams:

  • Use an integer array of size 26 to represent character frequencies.
  • Increment the count for each letter in the first word.
  • Decrement for each letter in the second word.
  • If all counts end up being zero, both words have the same characters and frequencies → they’re anagrams.

Complexity Analysis

ComplexityExplanation
Time ComplexityO(n × k) — where n is the number of words and k is the average length of each word, since we compare characters for each pair.
Space ComplexityO(1) — we only use a fixed-size array of 26 elements.

Optional Optimization

This solution is already efficient for the given constraints. If we wanted, we could use a HashMap of sorted words to pre-check anagrams, but since we only compare adjacent elements, this direct frequency-count approach is optimal.


PengWu@pengwu-k64
Loading

Loading discussion...

Hey! 👋

Got something to say?

or to leave a comment.