commit 4dae09e5a5dc85c0abab05de636071b572aed330 parent 3da4ee0462ca7e1d27aaf27c49a4414785168a25 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Wed, 3 May 2023 20:04:00 -0500 Completed remove all occurences using dart Diffstat:
| A | remove-all-occurences-of-a-substring/remove-all.dart | | | 32 | ++++++++++++++++++++++++++++++++ |
1 file changed, 32 insertions(+), 0 deletions(-)
diff --git a/remove-all-occurences-of-a-substring/remove-all.dart b/remove-all-occurences-of-a-substring/remove-all.dart @@ -0,0 +1,32 @@ +//Given the input "part" remove all occurences of it from +//the string. Each time an instance is found move back to the left +//and iterate through the list again. +//The worst case time complexity of this code is O(n^2) where +//Time: 276ms Beats: 100% +//Memory: 140.1MB Beats: 100% + +class Solution { + String removeOccurrences(String s, String part) { + int itr = 0; + while(itr != s.length){ + bool found = true; + for(int i = 0 ; i < part.length ; ++i){ + if(i + itr >= s.length){ + found = false; + break; + } + + if(s[i + itr] != part[i]){ + found = false; + break; + } + } + if(found){ + s = s.substring(0, itr) + s.substring(itr + part.length); + itr = -1; + } + itr += 1; + } + return s; + } +}