将来の参考のために、StackOverflow は通常、あなたが持っているものを投稿し、改善のための提案を求めることを要求します。
アクティブな日ではなく、退屈なので、あなたのためにこれを行いました. このコードは非常に効率的で、高度なデータ構造を使用していません。分かりやすくするためにこうしました。
私が何をしているかを理解しようとしてください。学習は StackOverflow の目的です。
学習を支援するために、コードにコメントを追加しました。
private String removeDuplicates(String keyword){
//stores whether a character has been encountered before
//a hashset would likely use less memory.
boolean[] usedValues = new boolean[Character.MAX_VALUE];
//Look into using a StringBuilder. Using += operator with strings
//is potentially wasteful.
String output = "";
//looping over every character in the keyword...
for(int i=0; i<keyword.length(); i++){
char charAt = keyword.charAt(i);
//characters are just numbers. if the value in usedValues array
//is true for this char's number, we've seen this char.
boolean shouldRemove = usedValues[charAt];
if(!shouldRemove){
output += charAt;
//now this character has been used in output. Mark that in
//usedValues array
usedValues[charAt] = true;
}
}
return output;
}
例:
//output will be the alphabet.
System.out.println(removeDuplicates(
"aaaabcdefghijklmnopqrssssssstuvwxyyyyxyyyz"));