🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of TithSambath

TithSambath's solution

to Anagram in the Java Track

Published at Aug 05 2020 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

AnagramTest.java

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Ignore;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

public class AnagramTest {


    @Test
    public void testNoMatches() {
        Anagram detector = new Anagram("diaper");

        assertThat(
            detector.match(
                Arrays.asList("hello", "world", "zombies", "pants")))
            .isEmpty();
    }

    @Ignore("Remove to run test")
    @Test
    public void testDetectMultipleAnagrams() {
        Anagram detector = new Anagram("master");

        assertThat(detector.match(Arrays.asList("stream", "pigeon", "maters")))
            .containsExactlyInAnyOrder​("maters", "stream");
    }

    @Ignore("Remove to run test")
    @Test
    public void testEliminateAnagramSubsets() {
        Anagram detector = new Anagram("good");

        assertThat(detector.match(Arrays.asList("dog", "goody"))).isEmpty();
    }

    @Ignore("Remove to run test")
    @Test
    public void testDetectLongerAnagram() {
        Anagram detector = new Anagram("listen");

        assertThat(
            detector.match(
                Arrays.asList("enlists", "google", "inlets", "banana")))
            .containsExactlyInAnyOrder​("inlets");
    }

    @Ignore("Remove to run test")
    @Test
    public void testDetectMultipleAnagramsForLongerWord() {
        Anagram detector = new Anagram("allergy");
        assertThat(
            detector.match(
                Arrays.asList(
                    "gallery",
                    "ballerina",
                    "regally",
                    "clergy",
                    "largely",
                    "leading")))
            .containsExactlyInAnyOrder​("gallery", "regally", "largely");
    }

    @Ignore("Remove to run test")
    @Test
    public void testDetectsMultipleAnagramsWithDifferentCase() {
        Anagram detector = new Anagram("nose");

        assertThat(detector.match(Arrays.asList("Eons", "ONES")))
            .containsExactlyInAnyOrder​("Eons", "ONES");
    }

    @Ignore("Remove to run test")
    @Test
    public void testEliminateAnagramsWithSameChecksum() {
        Anagram detector = new Anagram("mass");

        assertThat(detector.match(Collections.singletonList("last")))
            .isEmpty();
    }

    @Ignore("Remove to run test")
    @Test
    public void testCaseInsensitiveWhenBothAnagramAndSubjectStartWithUpperCaseLetter() {
        Anagram detector = new Anagram("Orchestra");

        assertThat(
            detector.match(
                Arrays.asList("cashregister", "Carthorse", "radishes")))
            .containsExactlyInAnyOrder​("Carthorse");
    }

    @Ignore("Remove to run test")
    @Test
    public void testCaseInsensitiveWhenSubjectStartsWithUpperCaseLetter() {
        Anagram detector = new Anagram("Orchestra");

        assertThat(
            detector.match(
                Arrays.asList("cashregister", "carthorse", "radishes")))
            .containsExactlyInAnyOrder​("carthorse");
    }

    @Ignore("Remove to run test")
    @Test
    public void testCaseInsensitiveWhenAnagramStartsWithUpperCaseLetter() {
        Anagram detector = new Anagram("orchestra");

        assertThat(
            detector.match(
                Arrays.asList("cashregister", "Carthorse", "radishes")))
            .containsExactlyInAnyOrder​("Carthorse");
    }

    @Ignore("Remove to run test")
    @Test
    public void testIdenticalWordRepeatedIsNotAnagram() {
        Anagram detector = new Anagram("go");

        assertThat(detector.match(Collections.singletonList("go Go GO")))
            .isEmpty();
    }

    @Ignore("Remove to run test")
    @Test
    public void testAnagramMustUseAllLettersExactlyOnce() {
        Anagram detector = new Anagram("tapper");

        assertThat(detector.match(Collections.singletonList("patter")))
            .isEmpty();
    }

    @Ignore("Remove to run test")
    @Test
    public void testWordsAreNotAnagramsOfThemselvesCaseInsensitive() {
        Anagram detector = new Anagram("BANANA");

        assertThat(detector.match(Arrays.asList("BANANA", "Banana", "banana")))
            .isEmpty();
    }

    @Ignore("Remove to run test")
    @Test
    public void testWordsOtherThanThemselvesCanBeAnagrams() {
        Anagram detector = new Anagram("LISTEN");

        assertThat(detector.match(Arrays.asList("Listen", "Silent", "LISTEN")))
            .containsExactlyInAnyOrder​("Silent");
    }

}
import java.util.ArrayList;
import java.util.List;

class Anagram {
    Anagram(String word){
        this.word = word;
    }
    private String word;
    private int gns; // Given Size of word
    private List<String> MatchWords = new ArrayList<>();
    List<String> match(List<String> CandidateWords){
        word = word.toLowerCase();
        MatchWords.addAll(CandidateWords);
        gns = word.length();
        // Step 1: Remove Candidate that size larger than given word
        MatchWords.removeIf(matchWords -> matchWords.length() != gns);
        // Step 2: Remove Candidate that is exactly equivalent to words
        MatchWords.removeIf(matchWords -> matchWords.compareToIgnoreCase(word) == 0);
        // Step 3: compare each letter
        int j = 0; // index each char
        int i = 0; // index each word
        StringBuilder temp = new StringBuilder(word); // temp is use to for compliment of word
        String tmp = word; // tmp here is use to store original given word without losing it
        while (i < MatchWords.size()){
            if (!word.contains(Character.toString(MatchWords.get(i).charAt(j)).toLowerCase())){
                // Remove candidate word from match word:
                MatchWords.remove(MatchWords.get(i));
                j = 0;
                word = tmp;
                temp.setLength(0);
                temp.append(word);
            }else {
                int idx = word.indexOf(MatchWords.get(i).charAt(j));
                if (idx != -1)
                    temp.setCharAt(idx,'*');
                    word = temp.toString();
                if (j + 1 < MatchWords.get(i).length())
                    j++;
                else {
                    i++;
                    j = 0;
                    word = tmp;
                    temp.setLength(0);
                    temp.append(word);
                }
            }
        }
        return MatchWords;
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?