package test2; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Utility2C { private Utility2C() { } /** * Returns the last character in the argument string. * * @param s a string * @return the last character in s * @pre. s is not empty */ public static char last(String s) { return s.charAt(s.length() - 1); } /** * Returns true if the string t is equal to the string s * in reverse, and false otherwise. For example, * *
* Utility2C.areReversed("abcd", "dcba") ** *
* returns true. * * @param s a non-null string * @param t a non-null string * @return true if t is equal to the string s * in reverse, and false otherwise * @throws IllegalArgumentException if the length of * s is not equal to the length of t */ public static boolean areReversed(String s, String t) { if (s.length() != t.length()) { throw new IllegalArgumentException(""); } for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != t.charAt(t.length() - i - 1)) { return false; } } return true; } /** * Returns a list of the characters that appear more than once * in the string s. The returned list contains no duplicate * characters. For example: * *
* Utility2C.repeatedChars("hi") returns the empty list * Utility2C.repeatedChars("EECS") returns the list ['E'] * Utility2C.repeatedChars("EECS2030") returns the list ['E', '0'] ** * @param s a non-null string * @return a list of the characters that appear more than once * in the string s */ public static List