Method (recursively) pure JS implemented in a string to find all positions b, a another string appear, and do not use the string

Question: A string is determined in index all strings B (length greater than 1) appears in. You may not use the string method indexof, substring, etc.

 

  A small partner in the interview encountered this problem, at first glance if you use use the string method indexof, substring, very simple and easy, but if you do not use these methods, how to achieve this demand it

 

  // idea: if the corresponding string methods can not be used, we can put a string into an array, using a recursive function constantly go over the corresponding array index, and then meet the conditions index print out, in fact, many are now front and rear end interactive processing methods of data, are used recursive ones, do not look down upon recursion!

Ado, let the solution to the problem:

<Script>
      // In fact, many methods are now the front and rear ends of interactive processing of data, are used in a recursive increases, do not look down recursive 
     @ ideas: string corresponding method can not be used, we can convert a string into an array, first, continue to use a recursive array index than the corresponding 
    // random characters 
    var str1 = 'adfacddtgjacbasaclsaacdctacw' ;
     // condition screening character 
    var str2 = 'basaclsa' ;
     // the corresponding strings into an array 
    var of arr1 = str1 .split ( '' );
     var arr2 is str2.split = ( '' );
     
    function Test (ARR) {
         // Write a for loop, a first array index arr2 first be screened brought alignments 
        for ( var I = 0; I <arr1.length; I ++ ) {
             // if so, performing the next layer 
            IF (ARR [0] === arr1 [I]) {
                 // enter here described: arr2 same first character index, and the index of the same character arr1 
                // Since the first index the same, we are here to declare a variable num, num variable depending on the length arr2 let go increments 
                var num = 0
                 function ccc (arr) {
                     // same as the first index, the index variable num add them separately, go over to them the latter position is the same as the index, so that if the condition is satisfied to continue incrementing num 
                    // num increments the value of the variable equal to the length of arr1 up, and this time the index of this description identical arr1 
                    IF (ARR [num] === arr1 [I + num]) {
                        if (num === arr.length-1) {
                            the console.log ( 'index are eligible' , I)
                        }
                        NUM ++
                         // the console.log (NUM) 
                        CCC (ARR)
                         // if the conditions are not met, the recursive let out 
                    } the else {
                         return
                    }
                }
                ccc(arr2)
            }
        }
    }
    test(str2)
</script>

 

  In fact, a recursive talking about, I think everyone not unfamiliar. For example, a childhood heard: There was once a mountain, the mountain there was a temple, the temple there was a monk, the monk in the story, once a mountain, the mountain there was a temple, the temple there was a monk, the monk in storytelling , once a mountain ...

  In fact, recursion, is to call their own in the course of operation. Program that calls itself recursively called programming skills (recursion). As a kind of recursive algorithm in a programming language widely applications. A procedure or function directly or indirectly calls itself a way, it is usually a large, complex problem into a layer similar to the original problem to solve smaller problems in its definition or description, Recursion only requiring less program can describe the problem-solving process is repeated calculation needed, greatly reducing the code size.

 

In fact this picture very vividly expressed the recursion.

  Well, recursive knowledge introduced almost finished. Yes! In simple terms, the cycle is never to return, and recursion is there to go back there (because of the termination condition).

Guess you like

Origin www.cnblogs.com/zhaohongcheng/p/11515659.html