indexOf principle, Java, javascript, python implementation

brief introduction

We found that the front-end and back-end projects of indexf are very frequent, no matter when we are working on them recently!

The meaning of indexOf: a given string matches the subscript of another string. If it matches, it returns the subscript. If it cannot match, it returns - 1. In fact, the principle is relatively simple. If you need to implement it, what should you do?

principle

Now, given the matching string A and the original string B, for example, the matching string A is equal to "practice while calling", and the original string B is equal to "practice while calling, I like to practice". You may find that "practice while calling" is the last two characters at A glance. We use B as A cycle, one word at A time to match. First find "call", find the counter and add 1, and then continue to find "practice". If you find that the next character is not "practice", the counter is cleared, Start searching again from "call". Now the penultimate character of B is found. The next character is "practice" Calculator Plus 1. At this time, the counter is equal to 2, which is exactly the length of A string. It shows that it is found. The search principle is such A process; Let's implement it in Java, javascript and python.

Note: the following code is not verified and is for reference only. python is the index method. The implementation principle is the same, but if it is not found, an error will be reported!

realization

Java implementation

public static void main(String[] args) {
        String orgin = "Practice while shouting. I like to practice while shouting";
        String serach = "Call practice";
        int index = indexOf(orgin,serach);
        System.out.println(index);
    }

/**
     *  indexOf Algorithm principle
     * @param orgin Original string B = "practice while calling, I like to practice";
     * @param serachString Matching string A = "practice"
     * @return int subscript
     */
public static int indexOf(String orgin,String serachString) {
    char[] chars = orgin.toCharArray();
    char[] sChars = serachString.toCharArray();
    //Return string subscript
    int index = -1;
    //The match string counter is used to query whether the complete string is matched
    int s_index = 0;
    //Global counter for calculating Subscripts
    int move = 0;
    for (int i=0; i<chars.length; i++) {
        move++;
        //If it matches "call", continue to go down and start matching "practice"
        if (chars[i] == sChars[s_index]) {
            s_index++;
            if(s_index == sChars.length) {
                index = move-sChars.length;
                break;
            }
        } else {
            s_index = 0;
        }
    }
    return index;
}

Javascript implementation

/**
     * @param orgin Original string B = "practice while calling, I like to practice";
     * @param serachString Matching string A = "practice"
     **/
    function indexOf(orgin,serachString) {
        //Return string subscript
        var index = -1;
        //The match string counter is used to query whether the complete string is matched
        var s_index = 0;
        //Global counter for calculating Subscripts
        var move = 0;
        for (var i=0; i<orgin.length; i++) {
            move++;
            //If it matches "call", continue to go down and start matching "practice"
            if (orgin.substr(i,1) == serachString.substr(s_index,1)) {
                s_index++;
                if(s_index == serachString.length) {
                    index = move-serachString.length;
                    break;
                }
            } else {
                s_index = 0;
            }
        }
        return index;
    }

python implementation

# Principle of indexOf algorithm
# @param orgin original string B = "practice while calling, I like to practice";
# @param serachString matching string A = "practice"
# @return int subscript
def index(orgin, serachString):
    # Return string subscript
    index = -1
    # The match string counter is used to query whether the complete string is matched
    s_index = 0
    # Global counter for calculating Subscripts
    move = 0
    for letter in enumerate(orgin):
        move = move + 1
        # If it matches "call", continue to go down and start matching "practice"
        if letter[1] == serachString[s_index]:
            s_index = s_index + 1
            if s_index == len(serachString):
                index = move - len(serachString)
                break
        else:
            s_index = 0;

    return index

Tags: Java Algorithm script

Posted by dbomb101 on Wed, 18 May 2022 20:40:42 +0300