简体   繁体   English

使用递归的indexOf方法

[英]indexOf method using recursion

I need to write a recursive method called indexOf that accepts two Strings as parameters and that returns the starting index of the first occurrence of the second String inside the first String (or -1 if not found). 我需要编写一个名为indexOf的递归方法,该方法接受两个String作为参数,并返回第一个String内第二个String第一次出现的起始索引(如果未找到则返回-1)。 I have to solve this problem using recursion. 我必须使用递归解决此问题。 These are some example results: 这些是一些示例结果:

indexOf("Barack Obama", "Bar")  0

indexOf("Barack Obama", "ck")  4

indexOf("Barack Obama", "a")  1

indexOf("Barack Obama", "McCain")  -1

indexOf("Barack Obama", "BAR")  -1

This is my solution but it gives me 6 for indexOf("Barack Obama", "McCain")instead of -1. 这是我的解决方案,但是它给我indexOf(“ Barack Obama”,“ McCain”)的6而不是-1。

public static int indexOf(String s1, String s2) {
        if(s1.equals(s2))
            return 0;

        else
            return indexOfHelper(s1, s2, 0); 
    }
private static int indexOfHelper(String s1, String s2, int ctr) {
        if(s2.length() > s1.length())
            return -1;

        if(s2.length() == 0 || s1.length() == 0)    //base case
            return ctr;     

        else                    //recursive case
            if(s1.charAt(0) == s2.charAt(0)){       //if there is a matching character
                if(s1.substring(0, s2.length()).equals(s2))
                    return ctr;     //if there is a matching character and the rest of the strings match as well
                else
                    return -1;        //if there is a matching character but the rest of the strings don't match
            }
            else
                return 1 + indexOfHelper(s1.substring(1), s2, ctr);

} }

I must say that recursive step was quite tricky. 我必须说,递归步骤非常棘手。 It passes all test cases. 它通过了所有测试用例。 I hope following helps. 希望以下内容对您有所帮助。 Java and C++ code here Java和C ++代码在这里

JAVA 爪哇

int indexOf (String s1, String s2) {

    if (s1.length() == 0 && s2.length() == 0) {
        return 0;
    } else if (s1.length() == 0) {
        return -1;
    } else if (s2.length() == 0) {
        return 0;
    } else if (s2.length()>s1.length()) {
        return -1;
    }
    else if (s1.charAt(0)  == s2.charAt(0)) {
        int subIndex = indexOf(s1.substring(1),s2.substring(1));
        return  subIndex == -1 ? -1: subIndex;


    } else {
        int subIndex = indexOf(s1.substring(1),s2);
        return  subIndex == -1 ? -1: subIndex+1;
    }

}

C++ code here 这里的C ++代码

int indexOf (string s1, string s2) {

        if (s1 == "" && s2 == "") {
            return 0;
        } else if (s1 == "") {
            return -1;
        } else if (s2=="") {
            return 0;
        } else if (s2.size()>s1.size()) {
            return -1;
        }
        else if (s1 [0] == s2[0]) {
            int subIndex = indexOf(s1.substr(1,s1.length()),s2.substr(1,s2.length()));
            return  subIndex == -1 ? -1: subIndex;


        } else {
            int subIndex = indexOf(s1.substr(1,s1.length()),s2);
            return  subIndex == -1 ? -1: subIndex+1;
        }

    }

Here is my solution 这是我的解决方案

public static void main(String[] args) {
    System.out.println(indexOf("Ninja!", "i"));
    System.out.println(indexOf("ninja2", "ja2"));
    System.out.println(indexOf("ninja2", "hae"));
}

public static int indexOf(String s, String contains) {
    if (contains.length() > s.length()) {
        return -1;
    }
    return indexOf(s, contains, 0);
}

private static int indexOf(String s, String contains, int index) {
    if ((s.length() - contains.length()) < index) {
        return -1;
    }
    if (s.substring(index, index + contains.length()).equals(contains)) {
        return index;
    } else {
    return indexOf(s, contains, index + 1);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM