leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

remove-all.dart (914B)


      1 //Given the input "part" remove all occurences of it from
      2 //the string. Each time an instance is found move back to the left
      3 //and iterate through the list again.
      4 //The worst case time complexity of this code is O(n^2) where 
      5 //Time: 276ms Beats: 100%
      6 //Memory: 140.1MB Beats: 100%
      7 
      8 class Solution {
      9   String removeOccurrences(String s, String part) {
     10       int itr = 0;
     11       while(itr != s.length){
     12           bool found = true;
     13           for(int i = 0 ; i < part.length ; ++i){
     14               if(i + itr >= s.length){
     15                   found = false;
     16                   break;
     17               }
     18               
     19               if(s[i + itr] != part[i]){
     20                   found = false;
     21                   break;
     22               }
     23           }
     24           if(found){
     25               s = s.substring(0, itr) + s.substring(itr + part.length);
     26               itr = -1;
     27           }
     28           itr += 1;
     29       }
     30       return s;
     31   }
     32 }