Skip to content

Instantly share code, notes, and snippets.

@futureperfect
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futureperfect/11018174 to your computer and use it in GitHub Desktop.
Save futureperfect/11018174 to your computer and use it in GitHub Desktop.
Answers for Gist located at practice programming problems gist
Problems can be found here:
https://gist.github.com/futureperfect/11016201
/**
* Source
*/
/**
* The strategy employed is to create a normalized representation of the strings
* (downcased and with whitespace removed) and populate a Multiset with the string's
* characters. If the Multisets are the same, then they are anagrams.
*
* For a good explanation of the virtues of Multisets, see:
* https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multiset
*/
package io.tense.interview;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.HashMultiset;
public class AnagramValidator {
public static boolean isAnagram(String s, String t) {
checkNotNull(s, "A null string cannot be an anagram, dingus");
checkNotNull(t, "A null string cannot be an anagram, dingus");
s = s.replaceAll("\\s", "").toLowerCase();
t = t.replaceAll("\\s", "").toLowerCase();
// easy optimization -- if strings aren't same length, can't be anagrams
if (s.length() != t.length()) {
return false;
}
HashMultiset<Character> sMultiset = HashMultiset.create();
HashMultiset<Character> tMultiset = HashMultiset.create();
for (int i = 0; i < s.length(); i++) {
sMultiset.add(s.charAt(i));
}
for (int i = 0; i < t.length(); i++) {
tMultiset.add(t.charAt(i));
}
return s.equals(t);
}
}
/**
* Tests
*/
package io.tense.interview;
import static org.junit.Assert.*;
import static io.tense.interview.AnagramValidator.isAnagram;
import org.junit.Test;
public class AnagramValidatorTest {
@Test(expected = NullPointerException.class)
public void testRaisesNPEWithBothElementsNull() {
isAnagram(null, null);
}
@Test(expected = NullPointerException.class)
public void testRaisesNPEWithFirstElementNull() {
isAnagram(null, "Something");
}
@Test(expected = NullPointerException.class)
public void testRaisesNPEWithLastElementNull() {
isAnagram("Something", null);
}
@Test
public void testTreatsEmptyStringsAsAnagrams() {
assertTrue(isAnagram("", ""));
}
@Test
public void testTreatsDistinctStringsAsNotAnagrams() {
assertFalse(isAnagram("Alice", "Bob"));
}
@Test
public void testTreatsStringsWhichAreIdenticalExceptWhitespaceAsAnagrams() {
assertTrue(isAnagram("racecar", "race car"));
}
@Test
public void testTreatsStringsWhichAreIdenticalExceptCapitalizationAsAnagrams() {
assertTrue(isAnagram("Alabama", "alabama"));
}
}
//Includes source and unit tests
/**
* The strategy here is to convert the string into a mutable entity,
* (a char[]), and walk the string form back to front, inserting the
* characters encountered into the result char[] front to back
*/
/**
* Source
* /
package io.tense.interview;
public class StringUtils {
/**
* Reverse a string. Ideally would prefer to use StringBuilder as this does
* not respect Unicode characters with surrogate pairs. That said,
* even in that case reverse can get weird.
*
* @param s
* input string
* @return reversed input string
*/
public static String reverse(String s) {
if (s == null) {
return null;
}
char[] result = s.toCharArray();
for (int i = 0; i < s.length(); i++) {
result[i] = s.charAt((s.length() - 1) - i);
}
return new String(result);
}
}
/**
* Unit tests
*/
package io.tense.interview;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
public class StringUtilsTest {
@Test
public void testReturnsNullWithNullInput() {
assertThat(StringUtils.reverse(null), is(nullValue()));
}
@Test
public void testReversesAnEmptyString() {
assertThat(StringUtils.reverse(""), is(""));
}
@Test
public void testReversesASingleCharacterString() {
assertThat(StringUtils.reverse("Q"), is("Q"));
}
@Test
public void testReversesAMultiCharacterString() {
assertThat(StringUtils.reverse("bicycle"), is("elcycib"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment