From a426d73ef0df085c67599f32a7fbaf50c7cbe1a6 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Tue, 4 Jun 2024 12:53:09 +0530 Subject: [PATCH 1/3] Added solution for find-the-index-of-the-first-occurrence-in-a-string --- .../0028-find-first-occurrence-in-string.md | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md b/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md new file mode 100644 index 000000000..a7224393b --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md @@ -0,0 +1,114 @@ +--- +id: find-first-occurrence-in-string +title: Find the Index of the First Occurrence in a String +difficulty: Easy +sidebar_label: FindFirstOccurrenceInString +tags: + - String +--- + +## Problem Description + +| Problem Statement | Solution Link | LeetCode Profile | +| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- | +| [Remove Duplicates from Sorted Array](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/) | [Remove Duplicates from Sorted Array Solution on LeetCode](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | + +## Problem Description + +Given two strings `needle` and `haystack`, return the index of the first occurrence of `needle` in `haystack`, or -1 if `needle` is not part of `haystack`. + +### Examples + +#### Example 1: + +- **Input:** + - `haystack = "sadbutsad"` + - `needle = "sad"` +- **Output:** `0` +- **Explanation:** + - "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0. + +#### Example 2: + +- **Input:** + - `haystack = "leetcode"` + - `needle = "leeto"` +- **Output:** `-1` +- **Explanation:** + - "leeto" did not occur in "leetcode", so we return -1. + +### Constraints + +- 1 <= `haystack.length`, `needle.length` <= 10^4 +- `haystack` and `needle` consist of only lowercase English characters. + +### Approach + +To solve the problem, we can use the following approach: + +1. **Sliding Window Technique:** + - We iterate through the `haystack` string with a window of size equal to the length of the `needle`. + - At each step, we check if the substring of the `haystack` starting at the current index and having the same length as the `needle` matches the `needle`. + - If a match is found, we return the current index. + - If no match is found after iterating through the entire `haystack`, we return -1. + +### Solution Code + +#### Python + +``` +class Solution(object): + def strStr(self, haystack, needle): + if not needle: + return 0 + if len(needle) > len(haystack): + return -1 + + for i in range(len(haystack) - len(needle) + 1): + if haystack[i:i + len(needle)] == needle: + return i + return -1 + + +``` + +#### Java + +``` +class Solution { + public int strStr(String haystack, String needle) { + if (needle.isEmpty()) return 0; + + for (int i = 0; i <= haystack.length() - needle.length(); i++) { + if (haystack.substring(i, i + needle.length()).equals(needle)) { + return i; + } + } + + return -1; + } +} +``` + +#### C++ + +``` +class Solution { +public: + int strStr(string haystack, string needle) { + if (needle.empty()) return 0; + + for (int i = 0; i <= haystack.length() - needle.length(); i++) { + if (haystack.substr(i, needle.length()) == needle) { + return i; + } + } + + return -1; + } +}; +``` + +### Conclusion + +The above solution efficiently finds the index of the first occurrence of a substring `needle` in the string `haystack`. It employs a sliding window technique to traverse the `haystack` string and compare substrings with the `needle`, providing a simple yet effective approach to solving the problem. From 6af066409c59c4dfedfd0c482ea8fc7e1edf1210 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Tue, 4 Jun 2024 12:56:54 +0530 Subject: [PATCH 2/3] Resovled error --- .../0000-0099/0028-find-first-occurrence-in-string.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md b/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md index a7224393b..3bbd18553 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md +++ b/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md @@ -39,8 +39,8 @@ Given two strings `needle` and `haystack`, return the index of the first occurre ### Constraints -- 1 <= `haystack.length`, `needle.length` <= 10^4 -- `haystack` and `needle` consist of only lowercase English characters. +- `1 <= haystack.length , needle.length <= 10^4 ` +- haystack and needle consist of only lowercase English characters. ### Approach @@ -68,8 +68,6 @@ class Solution(object): if haystack[i:i + len(needle)] == needle: return i return -1 - - ``` #### Java From ab91a523ec8181fece5dfbf1b95c0d6e89f89b5b Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Tue, 4 Jun 2024 13:03:22 +0530 Subject: [PATCH 3/3] Changed label name --- .../0000-0099/0028-find-first-occurrence-in-string.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md b/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md index 3bbd18553..51a43bbd7 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md +++ b/dsa-solutions/lc-solutions/0000-0099/0028-find-first-occurrence-in-string.md @@ -2,7 +2,7 @@ id: find-first-occurrence-in-string title: Find the Index of the First Occurrence in a String difficulty: Easy -sidebar_label: FindFirstOccurrenceInString +sidebar_label: 0028-FindFirstOccurrenceInString tags: - String ---