time Complexity of this Method
Anonymous User
76

Can someone please help me with the complexity of this method .
This method check if string t is substring of String s.

public static boolean substr(String s, String t) {
    
    if (t.length()==0 || s.length()==0)
        return false;
    
    if (t.length() > s.length())
        return false;;
    int j = 0;
    int k=0;
    for (int i = 0;i<s.length();i++) {
        if (s.charAt(i) == t.charAt(k)) {
            j = i;
            while (k<t.length() && j<s.length() && s.charAt(j) == t.charAt(k)) {
                j++;
                k++;
            }
            
            if (k==t.length())
                return true;
            else
                k=0;
        }
    }
    return false;
}
is T.C = O(N) or O(N^2) in worst case .
Comments (1)