# is substring of string

Check if string 2 is a substring of string 1. If so return position of start string

ex: " a b c d e" , " c d e" returns 2

**Solution:** iterative check

1. Check for first char of string 2
2. if found, check string 2. length amount of chars
3. if all chars match, return position

```
public int isSubstring(String one, String two){
    if (one == null || two == null || two.length() > one.length()) return -1;
    if (two.length() == 0) return 0;
    
    //check all possible positions of first char string two
    for (int i = 0; i <= (one.length - two.length); i++){
        int j = 0;
        while (j < two.length() && one.charAt(i + j) == two.charAt(j){
            j++;
        }
        if (j = two.length()) return i;
    }
    return -1;
}
        
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://llssff.gitbook.io/coding-problems/string/is-substring-of-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
