Created
July 13, 2013 13:19
-
-
Save danielvaughan/5990707 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.danielvaughan.ctci.strings; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: dvaughan | |
* Date: 13/07/2013 | |
* Time: 13:48 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class HasAllUniqueCharacters { | |
public boolean hasAllUniqueCharacters(final String string) | |
{ | |
if (string==null || string.length()==0) | |
{ | |
return false; | |
} | |
if (string.length()==1) | |
{ | |
return true; | |
} | |
String first = string.substring(0,1); | |
String rest = string.substring(1); | |
if (rest.contains(first)) | |
{ | |
return false; | |
} | |
else | |
{ | |
return hasAllUniqueCharacters(rest); | |
} | |
} | |
public static void main(String [] args) | |
{ | |
HasAllUniqueCharacters hauc = new HasAllUniqueCharacters(); | |
String uniqueString = "abcdefgh"; | |
String nonUniqueString = "abcdefab"; | |
System.out.println(uniqueString + " " + hauc.hasAllUniqueCharacters(uniqueString)); | |
System.out.println(nonUniqueString + " " + hauc.hasAllUniqueCharacters(nonUniqueString)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.danielvaughan.ctci.strings; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: dvaughan | |
* Date: 13/07/2013 | |
* Time: 14:00 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class Permutation { | |
public boolean isPermutation (final String stringA, final String stringB) | |
{ | |
if (stringA.length() != stringB.length()) | |
{ | |
return false; | |
} | |
Map<Character, Integer> aCharCount = new HashMap<Character, Integer>(); | |
Map<Character, Integer> bCharCount = new HashMap<Character, Integer>(); | |
for (int i=0; i<stringA.length(); i++) | |
{ | |
char aChar = stringA.charAt(i); | |
char bChar = stringB.charAt(i); | |
int currentACount = aCharCount.get(aChar)==null ? 0 : aCharCount.get(aChar); | |
int currentBCount = bCharCount.get(bChar)==null ? 0 : bCharCount.get(bChar); | |
aCharCount.put(aChar, currentACount++); | |
bCharCount.put(bChar, currentBCount++); | |
} | |
for (Character key : aCharCount.keySet()) | |
{ | |
if (aCharCount.get(key) != bCharCount.get(key)) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
public static void main(String [] args) | |
{ | |
Permutation permutation = new Permutation(); | |
String string = "abcdefgh"; | |
String permString = "hgfedcba" ; | |
String nonPermString = "abcdefab"; | |
System.out.println(permString + " " + permutation.isPermutation(string, permString)); | |
System.out.println(nonPermString + " " + permutation.isPermutation(string, nonPermString)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment