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 - I Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Substring Search | Practice Problem

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

Problem Statement

Given two strings s1 and s2, find the first occurrence of s2 in s1 as a substring.

Approach

A simple solution is to traverse the string s1 and check every substring of s1 which has a length equal to string s2. As soon as substring s2 is found return the starting index at which it is found.

Analysis

  • Time Complexity: O(n * m) where n and m are string lengths of s1 and s2 respectively.
  • Auxiliary Space Complexity: O(1)

Implementation

C++
int findStartIndexOfSubstring(string s1, string s2) {
   if (s1.size() < s2.size()) {
       return - 1;
   }
   for (int i = 0; i <= s1.size() - s2.size(); i++) {
       int substringFound = 1;
       for (int j = 0; j < s2.size(); j++) {
           if (s1[i + j] != s2[j]) {
               substringFound = 0;
               break;
           }
       }
       if(substringFound == 1) {
           return i;
       }
   }
   return - 1;
}
Java
class Solution {
   int findStartIndexOfSubString(String s1, String s2) {
       if (s1.length() < s2.length()) {
           return - 1;
       }
       for (int i = 0; i <= s1.length() - s2.length(); i++) {
           int substringFound = 1;
           for (int j = 0; j < s2.length(); j++) {
               if (s1.charAt(i + j) != s2.charAt(j)) {
                   substringFound = 0;
                   break;
               }
           }
           if(substringFound == 1) {
               return i;
           }
       }
       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 - III
Substring Search - II
Substring Search - IV
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