Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mission anagram #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions clean-code-challanges/src/main/java/Anagram.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* Given a word and a list of possible anagrams, select the correct sublist.
Expand All @@ -7,11 +10,27 @@
*/
public class Anagram {

public Anagram(String word) {
private final String word;

public Anagram(String word) {
this.word = word;
}

public List<String> match(List<String> candidates) {
return null;
List<String> foundAnagrams = new ArrayList<>();
char[] wordChars = this.word.toLowerCase().toCharArray();

Arrays.sort(wordChars);

for (String candidate : candidates) {
char[] candidateChars = candidate.toLowerCase().toCharArray();
Arrays.sort(candidateChars);
Comment on lines +26 to +27

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Da du sowieso zweimal sortieren musst, hätte ich dies in eine Methode umgewandelt z.b sortLetters(). So kannst du wiederholenden Code vermeiden. Zitat Marco Rensch: "Wiederholende Code isch schlecht!" ;-)

if(Arrays.equals(wordChars,candidateChars) && !this.word.equalsIgnoreCase(candidate)){
foundAnagrams.add(candidate);
}
}

return foundAnagrams;

}
}