Created
April 12, 2016 22:15
-
-
Save RichardCSantana-zz/3108d9d1231d526ab4cf8c703ca7a13c to your computer and use it in GitHub Desktop.
Solution for http://blog.gainlo.co/index.php/2016/04/08/if-a-string-contains-an-anagram-of-another-string/
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.example; | |
/** | |
* @author richardsantana | |
*/ | |
public class Solver { | |
public boolean checkAnagram(String phrase, String search) { | |
for (String substring : phrase.split(" ")) { | |
if(substring.length() == search.length()){ | |
Structure phraseStructure = new Structure(phrase); | |
Structure searchStructure = new Structure(search); | |
if(searchStructure.calculateHash() == phraseStructure.calculateHash()){ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
class Structure { | |
final private String value; | |
public Structure(String value) { | |
this.value = value; | |
} | |
public int calculateHash(){ | |
int result = 0; | |
for(char character : value.toCharArray()){ | |
result += new Integer(character); | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment