计算递归算法的运行时复杂度

时间:2015-12-01 17:55:20

标签: java algorithm performance recursion

我是计算算法的运行时复杂度的新手,并且通常在算法中有for循环时用于计算复杂性,并且不确定如何正确计算算法的复杂度,如它主要只是许多if语句的列表。

代码可以在下面看到,算法测试以查看String c是否是字符串a和b的有序shuffle。

public class StringShuffleTest {

        public static boolean isOrderedShuffle(String a, String b, String c){
            //boolean to determine if String c is an ordered shuffle.
            boolean isShuffled = false;
            //variables for the size of Strings a, b and c.
            int n = a.length();
            int m = b.length();
            int len = c.length();     

            //if the length of c is not the length of a + b return false.
            if (len != (n + m)){
                return isShuffled;
            }

            //if String c contains String b as a substring, then remove String b from c and make m = 0.
            if (c.contains(b)){
                c = c.replace(b, "");
                m = 0;
            }

            //if the length of a or b is 0, and c equals a or b, return true, otherwise,
            //return false.
            if (n == 0 || m == 0){
                if (c.equals(a) || c.equals(b)){
                    return true;
                }
                else
                    return isShuffled;
            }

            //if String a has length 1, remove a from String c and make String a empty.
            if (n == 1){
                    c = c.substring(0, c.indexOf(a.charAt(0))) + c.substring(c.indexOf(a.charAt(0)) +1);
                    a = "";
                    return isOrderedShuffle(a, b, c);

                }

            //An ordered shuffle of two given strings, a and b, is a string that can be formed by interspersing
            //the characters of a and b in a way that maintains the left-to-right order of the characters from each
            //string.

            //Recursive algorithm to determine if String c is an ordered shuffle of a and b.
            else
            if (c.indexOf(a.charAt(0)) >= 0){

                int indexOfFirsta = c.indexOf(a.charAt(0));
                int indexOfSeconda = c.indexOf(a.charAt(1));

                if (indexOfFirsta <= indexOfSeconda){
                c = c.substring(0, indexOfFirsta) + c.substring(indexOfFirsta +1);
                a = a.substring(1, n);
                    System.out.println(a);
                    System.out.println(c);                   
                return isOrderedShuffle(a, b, c);
                }

            else
                if (c.indexOf(b.charAt(0)) >= 0){
                        int indexOfFirstb = c.indexOf(b.charAt(0));
                        int indexOfSecondb = c.indexOf(b.charAt(1));

                        if (indexOfFirstb <= indexOfSecondb){
                            c = c.substring(0, indexOfFirstb) + c.substring(indexOfFirstb +1);
                            b = b.substring(1, m);
                            System.out.println(b);
                            System.out.println(c);

                        return isOrderedShuffle(a, b, c);

                    }
            }

            }
        return isShuffled;         
        }       

    public static void main(String[] args) {

        System.out.println(StringShuffleTest.isOrderedShuffle("cat", "castle", "castlecat")); 

    }

    }

如果c的第一个字母不等于a或b的第一个字母,最初的情况应该是t(n),我认为最好的运行时间复杂度应该是t(n)= 1 = n + m,如果必须比较String c中每个字母的字符。任何帮助或建议将不胜感激。

0 个答案:

没有答案