Part I There Can Be Only One 5 points Filenames Homework5Met
Solution
import java.util.*;
import java.lang.*;
import java.io.*;
class Homework5Methods
{
public static String eliminateDuplicates(String str)
{
boolean checked[] = new boolean[150]; //take booleans for each character of the string to mark it checked
StringBuilder sb = new StringBuilder(checked.length); // stringbuiler object of length checked
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i); //extract character one by one
if (!checked[ch]) // check if checked[] is true ie it is already traversed
{
checked[ch] = true; // check char for first time only
sb.append(ch); // put checked characters in string builder object
}
}
return sb.toString();
}
}
class homework5Driver
{
public static void main (String[] args)
{
System.out.println(\"Part 1: \");
String result;
result=Homework5Methods.eliminateDuplicates(\"abracadabra\");
System.out.println(result);
result=Homework5Methods.eliminateDuplicates(\"What\'s a Seawolf? I\'m a Seawolf!\");
System.out.println(result);
result=Homework5Methods.eliminateDuplicates(\"Stony Brook University\");
System.out.println(result);
result=Homework5Methods.eliminateDuplicates(\"AaBbCcDd\");
System.out.println(result);
result=Homework5Methods.eliminateDuplicates(\"\");
System.out.println(result); //displays empty string
}
}
Output:
Success time: 0.04 memory: 711168 signal:0

