We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Beautiful Binary String
Beautiful Binary String
+ 0 comments int beautifulBinaryString(string b) { int count=0, idx; for(int i=0; i<b.size(); i++){ idx=b.find("010"); if(idx < b.size()){ b[idx+2]='1'; count++; } } return count; }
+ 0 comments here is my C solution: int c=0,i=0; int beautifulBinaryString(char* b) { if(b[i]!='\0') { if(b[i]=='0' && b[i+1]=='1' && b[i+2]=='0') { c++; i=i+3; } else{i++;} beautifulBinaryString(b); } return c; } *
+ 0 comments Here are my C++ solution: int beautifulBinaryString(string b) { int i = 0, d = 0; while (i < b.length() - 2) { if (b[i] == '0' && b[i + 1] == '1' && b[i + 2] == '0') { d++; i += 3; // Skip to the character after "010" } else { i++; } } return d; }
+ 0 comments Here are my c++ solution, explanation here : https://youtu.be/xf0JsPRH5rs
Solution 1 :
int beautifulBinaryString(string b) { int ans = 0; for(int i = 0; i < b.size();){ string sub = b.substr(i, 3); if(sub == "010") { ans++; i+=3; }else i++; } return ans; }
Solution 2 :
int beautifulBinaryString(string b) { regex re("010"); string c = regex_replace(b, re, ""); return (b.size() - c.size()) / 3; }
+ 0 comments n=int(input()) s=input() v='010' a=s.count(v) print(a)
Load more conversations
Sort 793 Discussions, By:
Please Login in order to post a comment