Substring algorithm

后端 未结 11 1292
小蘑菇
小蘑菇 2021-02-11 03:38

Can someone explain to me how to solve the substring problem iteratively?

The problem: given two strings S=S1S2S

相关标签:
11条回答
  • 2021-02-11 03:49

    Though its pretty old post, I am trying to answer it. Kindly correct me if anything is wrong,

    package com.amaze.substring;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class CheckSubstring {
    
    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
    
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
        System.out.println("Please enter the main string");
        String mainStr = br.readLine();
    
        System.out.println("Enter the substring that has to be searched");
        String subStr = br.readLine();
    
        char[] mainArr = new char[mainStr.length()];
        mainArr = mainStr.toCharArray();
        char[] subArr = new char[subStr.length()];
        subArr = subStr.toCharArray();
        boolean tracing = false;
        //System.out.println("Length of substring is "+subArr.length);
        int j = 0;
    
        for(int i=0; i<mainStr.length();i++){
    
            if(!tracing){
                if(mainArr[i] == subArr[j]){
                    tracing = true;
                    j++;
                }
            } else {
                if (mainArr[i] == subArr[j]){
                    //System.out.println(mainArr[i]);
                    //System.out.println(subArr[j]);
                    j++;
                    System.out.println("Value of j is "+j);
                    if((j == subArr.length)){
                        System.out.println("SubString found");
                        return;
                    }
                } else {
                    j=0;
                    tracing = false;
                }
            }
        }
    
        System.out.println("Substring not found");
    
    }
    
    }
    
    0 讨论(0)
  • 2021-02-11 03:53

    This may be redundant with the above list of substring algorithms, but I was always amused by KMP (http://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm)

    0 讨论(0)
  • 2021-02-11 03:53

    Here is my PHP variation that includes a check to make sure the Needle does not exceed the Haystacks length during the search.

    <?php
    
    function substring($haystack,$needle) {
            if("" == $needle) { return true; }
            echo "Haystack:\n$haystack\n";
        echo "Needle:\n$needle\n";
    
            for($i=0,$len=strlen($haystack);$i<$len;$i++){
                    if($needle[0] == $haystack[$i]) {
                            $found = true;
                            for($j=0,$slen=strlen($needle);$j<$slen;$j++) {
                                    if($j >= $len) { return false; }
                                    if($needle[$j] != $haystack[$i+$j]) {
                                            $found = false;
                                            continue;
                                    }
                            }
                            if($found) {
                                    echo " . . . . . . SUCCESS!!!! startPos: $i\n";
                                    return true;
                            }
                    }
            }
            echo " . . . . . . FAILURE!\n" ;
            return false;
    }
    
    assert(substring("haystack","hay"));
    assert(!substring("ack","hoy"));
    assert(substring("hayhayhay","hayhay"));
    assert(substring("mucho22","22"));
    assert(!substring("str","string"));
    ?>
    

    Left in some echo's. Remove if they offend you!

    0 讨论(0)
  • 2021-02-11 03:54
    // runs in best case O(n) where no match, worst case O(n2) where strings match
    
    var s = "hippopotumus"
    var t = "tum"
    
    for(var i=0;i<s.length;i++)
        if(s[i]==t[0])
            for(var ii=i,iii=0; iii<t.length && i<s.length; ii++, iii++){
                if(s[ii]!=t[iii]) break
                else if (iii==t.length-1) console.log("yay found it at index: "+i)
            }
    
    0 讨论(0)
  • 2021-02-11 03:56

    It would go something like this:

    m==0? return true
    cs=0
    ct=0
    loop
        cs>n-m? break
        char at cs+ct in S==char at ct in T?
        yes:
            ct=ct+1
            ct==m? return true
        no:
            ct=0
            cs=cs+1
    
    end loop
    return false
    
    0 讨论(0)
  • 2021-02-11 03:58

    I know I'm late to the game but here is my version of it (in C#):

        bool isSubString(string subString, string supraString)
        {
            for (int x = 0; x <= supraString.Length; x++)
            {
                int counter = 0;
                if (subString[0] == supraString[x]) //find initial match
                {
                    for (int y = 0; y <= subString.Length; y++)
                    {
                        if (subString[y] == supraString[y+x])
                        {
                            counter++;
                            if (counter == subString.Length)
                            {
                                return true;
                            }
                        } 
                    }
                }
            }
            return false;
        }
    
    0 讨论(0)
提交回复
热议问题