kloggey
BAN USERI just add a main method to run it and put it in my IDE and it works perfectly. (There were a few bugs, but I haven't hand written code in forever!)
public class AnagramChecker {
// Find whether String b contains a subset of an anagram of String a:
public Boolean hasAnagram (String n, String m) {
char A[] = n.toCharArray();
char B[] = m.toCharArray();
// Search B for A backwards, return true if found
for (int b = 0; b < ( B.length - A.length + 1 ); b++) { // Loop through B
for (int a = ( A.length - 1 ); a >= 0; a -- ) { // Loop through A
if( B[b] == A[a]) { // Check
b++; // Check the next cell
if( a< 1 )
return true; // If all checks worked, return true
}
else { // If a check failed, reset values
b = b - ((A.length - 1) - a);
a = 0;
}
}
}
// If no anagram found, return false
return false;
}
// Test the function:
public static void main (String[] args) {
AnagramChecker AC = new AnagramChecker();
String first = "supercali";
String second = "fragilistic";
String third = "expialidocious";
String anagram = "ila";
System.out.println("ali is an anagram of ila");
if(AC.hasAnagram(anagram, first))
System.out.println(first + " contains the anagram");
else System.out.println(first + " does not contain the anagram");
if(AC.hasAnagram(anagram, second))
System.out.println(second + " contains the anagram");
else System.out.println(second + " does not contain the anagram");
if(AC.hasAnagram(anagram, third))
System.out.println(third + " contains the anagram");
else System.out.println(third + " does not contain the anagram");
}
}
*EDITED*
I don't know much about time complexities and whatnot, but this definitely works!
public class AnagramChecker {
// Find whether n contains a subset of an anagram of m:
public Boolean hasAnagram (String n, String m) {
char A[] = n.toCharArray();
char B[] = m.toCharArray();
boolean index[] = new boolean [A.length];
int counter = 0;
// Search B for an anagram of A, return true if found
for (int b = 0; b < ( B.length - A.length + 1); b++) { // Loop through B
for (int d = 0; d < index.length; d++) // Reset the record
index[d] = false;
for (int a = 0; a < A.length; a ++ ) { // Loop through A
for (int c = b; c < ( b + A.length ); c++) { // Loop through the possible Bs
if (!index[a]) { // If the A char has not been matched
if ( A[a] == B[c] ) { // If there is a match,
index[a] = true; // record the match
if ( a == (A.length - 1)) { // If it is the last A char to match
for (int d = 0; d < index.length; d++) { // Check to see if all As matched
if (index[d])
counter++;
}
if ( counter == index.length )
return true; // If all matched, return true
counter=0; // Reset the counter if no match found
}
}
}
}
}
}
// If no anagram found, return false
return false;
}
// Test the function:
public static void main (String[] args) {
AnagramChecker AC = new AnagramChecker();
String first = "supercali";
String second = "fragilistic";
String third = "expialidocious";
String anagram = "dial";
if(AC.hasAnagram(anagram, first))
System.out.println(first + " contains an anagram of dial");
else System.out.println(first + " does not contain an anagram of dial");
if(AC.hasAnagram(anagram, second))
System.out.println(second + " contains an anagram of dial");
else System.out.println(second + " does not contain an anagram of dial");
if(AC.hasAnagram(anagram, third))
System.out.println(third + " contains an anagram of dial");
else System.out.println(third + " does not contain an anagram of dial");
}
}
Reprizzafilher, Java Developer at Achieve Internet
Sorter with 3+ years of experience , performs duties in a safe manner in compliance with all local, state, and federal ...
Repreetaharriet, Applications Developer at Achieve Internet
As a sales clerk, I deal with customers on a daily basis. My work face-to-face assisting customers with finding the ...
Repvickgonder9, Integration Software Engineer at Arista Networks
I am Antonio , a well-experienced Sales Representative who has been working in the Suadela Investment for almost 2 years. I ...
RepAriAllen, AT&T Customer service email at ABC TECH SUPPORT
I have experience with collecting data and conducting experiments for studies in a variety of industries. I am a respected ...
RepAtiLee, abc at A9
Hard-working and dependable construction site supervisor with 2 years of success in the industry.Exceptional leadership ability, helping me keep ...
That's really funny, I should look these things up!
- kloggey March 24, 2014