Ajuda!

2 respostas
V

:?: Po favor alguem poderia me dar uma dica de como faço um programa que apartir de uma string gera todos os anagramas da palavra.
Ex: casa ,asac, saca,acas etc :?:

2 Respostas

J

Especifique melhor sua pergunta…
num posso nem tentar te ajudar pq nem sei o q eh um anagrama :smiley:
hehehe

F

n sei se eh bem isso q vc quer… mas tenta isso:

public String[] getAnagrama(String str){
 int num = 1;
 for (int i = 0; i < str.length(); i++){
  num *= (str.length() - num + 1);
  if (isRepetido(str, i)) num--;
 }
 String anagrama[] = new String[num];
 for (int i = 0; i < num; i++){
  anagrama[i] = "";
 }
 int aux = 0;
 for (int i = 0; i < num; i++){
  aux = 0;
  while (jahTem(anagrama, str){
   if (aux >= str.length()) aux = 0;
   str = troca(str, aux++, i);
  }
  anagrama[i] = str;
 }
 return anagrama;
}
private String troca(String str, int x, int y){
 if (x == y) return str;
 char c = str.charAt(x);
 if (x <= 0) str = str.charAt(y) + str.substring(1);
 else if (x >= str.length()) str = str.substring(0, x - 1) + str.charAt(y);
 else str = str.substring(0, x - 1) + str.charAt(y) + str.substring(x + 1);
 if (y <= 0)  str = c + str.substring(1);
 else if (y >= str.length()) str = str.substring(0, y - 1) + c;
 else str = str.substring(0, y - 1) + c + str.substring(y + 1);
 return str;
}
private boolean jahTem(String a[], String str){
 for (int i = 0; i < a.length; i++){
  if (a[i].equals(str)) return true;
 }
 return false;
}
private boolean isRepetido(String str, int i){
 char c = str.charAt(i);
 for (i--; i >= 0; i--){
  if (str.charAt(i) == c) return true;
 }
 return false;
}

n testei pra ver se funciona… se vc quer o q eu to pensando, deve ser ± por esse caminho…

Criado 22 de abril de 2004
Ultima resposta 22 de abr. de 2004
Respostas 2
Participantes 3