Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Substring Search - IV Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Substring Search - IV

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given two strings s1 and s2, find the index of the first occurrence of s2 in s1 as a substring.
If no such occurrence exists, return -1. This problem is also known as finding a needle in a haystack. Use the Z-algorithm to solve this problem.

Approach

The Z function is an algorithm that takes a string as an input, and return a list of integers of size same as that of the string, such that Z[i] will represent the length of the longest matching prefix of the original string, which matches with the string if it had started at the ith position, and all of it is done in linear time.

The algorithm to solve this problem using the Z algorithm is as follows:

  • Concatenate the two strings pattern and original into a 3rd string, separating them by a character not present in either of the strings.
  • Compute the Z function of this new string.
  • Traverse through the Z array, and check if at any point Z[i] = Length of the pattern, if yes, return the index as i - length of pattern - 1.
  • If such a point is not found, return -1.

Construct Z Array in Linear Time: 

To compute the Z function in linear time, we try to maintain an interval marked by pointers left and right, so that the right end of the interval is maximum possible and the range [left, right] is a prefix of the original string.

The key idea is:

  • If we are outside this interval, we reset the values of left and right and start computing the interval again, and set Z[i] = right - left + 1 directly.
  • Else we try to compute the Z function by trying to maximize the right end of the interval and setting Z[i] = min(Z[i - left], right - i + 1).

Analysis:

  • Time Complexity: O(n + m) // n = length of 1st string, m = length of 2nd string.
  • Space Complexity: O(n + m)

Implementation

C++
vector<int> zFunction(string s, int n) {
    vector<int> z(n);
    int left = 0, right = 0;
    for(int i = 1; i < n; i++) {
        if(i <= right) {
            z[i] = min(right - i + 1, z[i - left]);
        }
        while(i + z[i] < n && s[z[i]] == s[z[i] + i]) {
            z[i]++;
        }
        if(i + z[i] - 1 > right) {
            left = i;
            right = i + z[i] - 1;
        }
    }
    return z;
}
int findStartIndexOfSubstring(string s1, string s2) {
	string mixed = s2 + "$" + s1;
    vector<int> getZ = zFunction(mixed, mixed.length());
	for(int i = 0; i < mixed.length(); i++) {
		if(getZ[i] == s2.length()) {
			return (i - s2.length() - 1);
		}
	}
	return -1;
}
Java
class Solution {
	int[] zFunction(char[] s, int n) {
		int[] z = new int[n];
		int left = 0, right = 0;
		for(int i = 1; i < n; i++) {
			if(i <= right) {
				z[i] = Math.min(right - i + 1, z[i - left]);
			}
			while(i + z[i] < n && s[z[i]] == s[z[i] + i]) {
				z[i]++;
			}
			if(i + z[i] - 1 > right) {
				left = i;
				right = i + z[i] - 1;
			}
		}
		return z;
	}
    int findStartIndexOfSubstring(String s1, String s2) {
        String mixed = s2 + "$" + s1;
		char[] aux = mixed.toCharArray();
		int[] getZ = zFunction(aux, mixed.length());
		for(int i = 0; i < mixed.length(); i++) {
			if(getZ[i] == s2.length()) {
				return (i - s2.length() - 1);
			}
		}
		return -1;
    }
}
Related Content
Anagrams
Compare Version Numbers
Count And Say
Integer to Roman Numeral
Longest Common Prefix
Longest Palindrome in String
Insert Minimum To Make Palindrome
Reverse Words in String
Roman Numeral to Integer
Substring Search - I
Substring Search - III
Substring Search - II
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet