View Full Version : anagrams recursion

04-12-2008, 11:23 PM
I can't use any for or while loops. This is what I have in Python:

def anagrams(s):
if s == "":
return [s]
ans = []
for w in anagrams(s[1:]):
for pos in range(len(w)+1):
return ans

Also how do I convert this code to java? For example, the for loop:

for w in anagrams(s[1:]):

04-13-2008, 09:09 AM
anyone? I'm been stuck on this problem for days. I have no idea how to find the anagrams without using for or while loops. Someone please help. I would think that one would need to use a for loop to add each starting letter to each position between the characters of the string. Am I correct?


04-15-2008, 06:15 AM
Why not use a for loop?

As for the java for-in loop, the syntax is like so:

// abstractly
for(T t : TCollectionOrTArray) {
. . .

// concretely
String[] strs = new String[] {"a", "ab", "abc"};
for(String str : strs) {

04-15-2008, 05:17 PM
Can you explain the goal of the program a bit more? What exactly are you trying to do?

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum