leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 8fb36e0246c9b49f5ca0f4984fabbf633949cde9
parent f70c862f2ab91fe82a4550ba033cf78317552e24
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Wed, 19 Apr 2023 00:54:40 -0500

Completed find the index of substring problem using dart

Diffstat:
Afind-the-index-of-the-first-occurence/find-index-of-first-occurence.dart | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+), 0 deletions(-)

diff --git a/find-the-index-of-the-first-occurence/find-index-of-first-occurence.dart b/find-the-index-of-the-first-occurence/find-index-of-first-occurence.dart @@ -0,0 +1,51 @@ +//Find the first occurence of a substring in a given string +//if the substring is not found return -1. I completed this using +//the sliding window method of checking if any given character is the word +//and then moving to the next charcter if it is not. This is an O(M * N) time +//complexity function. +//Runtime: 206ms Beats: 99.45% +//Memory: 139.3MB Beats: 87.91% + + + + +void main(){ + Solution sol = new Solution(); + print(sol.strStr("Test input raoiset", "input")); +} + +class Solution { + int strStr(String haystack, String needle) { + + if(haystack == needle){ + return 0; + } + if(needle.length == 1){ + for(int i = 0; i < haystack.length ; ++i){ + if(haystack[i] == needle){ + return i; + } + } + return -1; + } + int x = 0; + for(int i = 0; i < haystack.length; ++i){ + if(haystack[i] == needle[0]){ + int count = 1; + for(x = i + 1; x < haystack.length ; ++x){ + if(count == needle.length){ + return x - needle.length; + } + if(haystack[x] != needle[count]){ + break; + } + count += 1; + } + if(count == needle.length){ + return x - needle.length; + } + } + } + return -1; + } +}