Untitled
class Solution { public: int secondsToRemoveOccurrences(string s) { string newStr= s; int strSize= s.size(); bool processed= false; int seconds=0; while(!processed){ bool found01= false; for(int i=1;i<strSize;i++){ if(s[i]=='1' && s[i-1]=='0'){ newStr[i]= '0'; newStr[i-1]= '1'; found01= true; } } if(!found01) processed= true; else seconds++; s= newStr; } return seconds; } };
Leave a Comment