0

Consider you have been told to implement a kind of the java String.substring method.
The signature of the method is as follows:

public static boolean isSubstring(String i_StringForSearch, String i_SubStringToFind)

Here is my solution, but I feel it's still not the best elegant solution I could have.(Many canonical if else's)
What do you think? Would you do it in another way?

public static boolean isSubstring(String i_StringForSearch, String i_SubStringToFind)
    {
        int strForSearchIndex = 0;
        int subStrToFindIndex = 0;
        boolean endOfStringToSearch = false;
        boolean foundSubString = false;
        boolean isThereASequenceOfMatching = false;


        while(!endOfStringToSearch && !foundSubString)
        {
            if(strForSearchIndex == i_StringForSearch.length())
            {
                endOfStringToSearch = true;
            }

            else if(i_StringForSearch.charAt(strForSearchIndex) == i_SubStringToFind.charAt(subStrToFindIndex))
            {
                isThereASequenceOfMatching = true;
                if(subStrToFindIndex == i_SubStringToFind.length() -1 )
                {
                    foundSubString = true;
                }
                subStrToFindIndex++;
                strForSearchIndex++;
            }

            else if(i_StringForSearch.charAt(strForSearchIndex) != i_SubStringToFind.charAt(subStrToFindIndex))
            {
                if(isThereASequenceOfMatching)
                {
                    subStrToFindIndex = 0;
                    isThereASequenceOfMatching = false;
                }
                strForSearchIndex++;
            }
        }

       return foundSubString;
    }
7
  • 3
    You may want to move this to codereview.stackexchange.com for better responses! (and also tag it as homework if it is so)! Commented Sep 3, 2012 at 17:37
  • 4
    That looks more like contains than substring to me. substring has a very clearly understood purpose, which is very different from what's here. Commented Sep 3, 2012 at 17:38
  • Why are you re-inventing the wheel? Commented Sep 3, 2012 at 17:38
  • When you are told to implement something which is the same as or can use a common built API, it doesn't make sense to rewrite it from scratch. Commented Sep 3, 2012 at 17:41
  • Because It's a good exercise , and if you were the man who are told to write it in the first time what would you say then? Commented Sep 3, 2012 at 17:42

2 Answers 2

1

Look up the Boyer-Moore and Knuth-Morris-Pratt algorithms. In tests many years ago I found BM to be slightly faster.

Sign up to request clarification or add additional context in comments.

2 Comments

I just saw that complexity of KMP algo is Θ(n+m) when n is size of String/Text to search for, and m is the size of the substring to search. Boyer-Moore is Θ(nm) so it is supposed to be worse. But What about my solution complexity? Doesn't it Θ(n+m) also?
@JavaSa I don't know what you're asking for now. You asked for the most elegant solutions, there they are. Unless you can show yours is better than either it's of no interest. It's up to you.
0
public static boolean isSubstring(final String i_StringForSearch, final String i_SubStringToFind) {
    int j = 0;
    for (int i = 0; i < i_StringForSearch.length(); i++) {
        if (i_StringForSearch.charAt(i) == i_SubStringToFind.charAt(j)) {
            j++;
            if (j == i_SubStringToFind.length()) {
                return true;
            }
        }
    }
    return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.