"Tree"
package main;
import java.util.Arrays;
import java.util.Scanner;
import java.util.ArrayList;
public class HomeB{
public static void main(String[]args) {
Scanner scan = new Scanner(System.in);
String input = "";
String inputb = "";
System.out.println("First string?");
input = scan.nextLine();
System.out.println("Second string?");
inputb = scan.nextLine();
System.out.println(isAnagram(input, inputb));
scan.close();
}
public static boolean isAnagram(String a, String b) {
ArrayList<String>list = new ArrayList<String>();
list.addAll(Arrays.asList(a.split("")));
return recurse("", list, b);
}
public static boolean recurse(String current, ArrayList<String> entry, String tocompare) {
if (entry.size()>0) {
boolean correct = false;
for (int i=0; i<entry.size(); i++) {
ArrayList<String> next = new ArrayList<String>();
next.addAll(entry);
next.remove(i);
if (recurse(current + entry.get(i), next, tocompare)) {
correct=true;
}
}
return correct;
} else {
return current.equals(tocompare);
}
}
}
import java.util.Arrays;
import java.util.Scanner;
import java.util.ArrayList;
public class HomeB{
public static void main(String[]args) {
Scanner scan = new Scanner(System.in);
String input = "";
String inputb = "";
System.out.println("First string?");
input = scan.nextLine();
System.out.println("Second string?");
inputb = scan.nextLine();
System.out.println(isAnagram(input, inputb));
scan.close();
}
public static boolean isAnagram(String a, String b) {
ArrayList<String>list = new ArrayList<String>();
list.addAll(Arrays.asList(a.split("")));
return recurse("", list, b);
}
public static boolean recurse(String current, ArrayList<String> entry, String tocompare) {
if (entry.size()>0) {
boolean correct = false;
for (int i=0; i<entry.size(); i++) {
ArrayList<String> next = new ArrayList<String>();
next.addAll(entry);
next.remove(i);
if (recurse(current + entry.get(i), next, tocompare)) {
correct=true;
}
}
return correct;
} else {
return current.equals(tocompare);
}
}
}
It's funny, 'cause the code branches
No comments:
Post a Comment