Including a recursive function in a for?

Does every recursive function have an equivalent for a loop? (Both achieve the same result).

I have this recursive function:

private static boolean recur(String word, int length) {
    if(length == 1 || length == 2)
        return false;
    if(length == 0)
        return true;
    if(words[length].contains(word.substring(0, length)))
        return recur(word.substring(length), word.length() - length);
    return recur(word, length-1);
}

Given that the words are Set [] and where the words [i] = a set with words of length i.

What I'm trying to do: initiate recursion with a word (say, "stackoverflow", without spaces), I'm trying to find out if this word can be cut into subwords ("stack", "over", "stream") .. the minimum length of the subword is 3, and given that the subword of length I is in the words Set [i].

I can confirm that this code works, but it may have a memory problem, so I want to rotate it in a loop .. if possible.

Do you need more information?

Thank.

+3
5

, , .

private static boolean recur(String word, int length) {
    if(length == 1 || length == 2)
        return false;
    if(length == 0)
        return true;
    int nextLength;
    String nextWord;
    if(words[length].contains(word.substring(0, length))) {
      nextWord = word.substring(length);
      nextLength = word.length() - length;
    } else {
      nextWord = word;
      nextLength = length - 1;
    }
    return recur(nextWord, nextLength);
}

. , :

private static boolean recur(String word, int length) {
    int nextLength = length;
    String nextWord = word;
    while( true ) {
        if(nextLength == 1 || nextLength == 2)
            return false;
        if(nextLength == 0)
            return true;
        if(words[nextLength].contains(nextWord.substring(0, nextLength))) {
            nextWord = nextWord.substring(nextLength);
            nextLength = nextWord.length() - nextLength;
        } else {
            nextWord = nextWord;
            nextLength = nextLength - 1;
        }
    }
}

, , "" .

+6

: , . , , .

:

, , false.

split, :

have a list with the word to split.
iterate until list after iteration is the same as before.
    iterate over list
        if the word can be split, replace it in the list with its parts
    end of loop
end of loop
+5

: , , , , , .

:

-, :

private static boolean recur(String word, int length) {
    while(true) {
        if(length == 1 || length == 2)
            return false;
        if(length == 0)
            return true;
        if(words[length].contains(word.substring(0, length))) {
            int newlen = word.length() - length;
            word = word.substring(length);
            length = newlen;
        }
        else {
            --length;
        }
    }
}

, .

, , !

( , , , - .)

, ABCDEFGH, ABCD, EFGH ABCDE - , FGH - . recur , ABCDE, , FGH , ABCDEFGH .

ABCD EFGH! , , . ( , , ABC , DEFGH - .)

:

private static boolean recur(String word, int length) {
    if(length == 1 || length == 2)
        return false;
    if(length == 0)
        return true;
    return (words[length].contains(word.substring(0, length)))
            && recur(word.substring(length), word.length() - length))
           || recur(word, length-1);
}

: . , , . , .

+2

: , . .

CPS ( , Java). (, ) .

+1

@biziclop , , . , .

  • length .
  • .
  • .

private static boolean recur(String word) {
    LOOP: for(;;) {
        for (int len = word.length(); len >= 3; len--)
            if (words[len].contains(word.substring(0, len))) {
                word = word.substring(len);
                continue LOOP;
            }
        return word.length() == 0;
    }
}
0

Source: https://habr.com/ru/post/1791185/


All Articles