Last active
May 24, 2018 05:01
-
-
Save anthonylgf/71f7cbea02926f3b59ca648ec42e3be6 to your computer and use it in GitHub Desktop.
atividade para desafio-06 osprogramadores.com
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
import java.util.TreeSet; | |
import java.util.ArrayList; | |
class Anagrama{ | |
static char[] aux; | |
static TreeSet<String> listaPermutacao = new TreeSet<>(); | |
static ArrayList<Tarefa> listaTarefa = new ArrayList<>(); | |
public static void main(String args[]){ | |
String word = ""; | |
if(args.length < 1){ | |
System.out.println("Execute como java Anagrama 'palavra'"); | |
System.exit(0); | |
} | |
for(String in : args){ | |
word += "" + in; | |
} | |
permutacao(criandoArray(word)); | |
} | |
public static String criandoArray(String palavra){ | |
String word = ""; | |
for(char car:palavra.toUpperCase().toCharArray()){ | |
if((car<=122 && car>=97) || (car<=90 && car>=60)) | |
word += "" + car; | |
else{ | |
System.out.println("Caractere invalido"); | |
System.exit(0); | |
} | |
} | |
return word; | |
} | |
public static void permutacao(String str) { | |
permutacao("", str); | |
} | |
private static void permutacao(String prefix, String str) { | |
int n = str.length(); | |
if (n == 1 && listaPermutacao.add(prefix+str)) System.out.println(prefix+str); | |
else { | |
for (int i = 0; i < n; i++) | |
listaTarefa.add(new Tarefa(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n), | |
listaPermutacao)); | |
for(Tarefa tar:listaTarefa) | |
tar.start(); | |
} | |
} | |
} | |
class Tarefa extends Thread{ | |
String arg1, arg2; | |
static TreeSet obj; | |
public Tarefa(String arg1, String arg2, TreeSet ana){ | |
this.arg1 = arg1; | |
this.arg2 = arg2; | |
this.obj = ana; | |
} | |
@Override | |
public void run(){ | |
this.permutacao(arg1,arg2); | |
} | |
private void permutacao(String prefix, String str) { | |
int n = str.length(); | |
if (n == 1 && adicionar(prefix+str)) System.out.println(prefix+str); | |
else { | |
for (int i = 0; i < n; i++) | |
permutacao(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n)); | |
} | |
} | |
private synchronized boolean adicionar(String prefix){ | |
return obj.add(prefix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment