Skip to content

Instantly share code, notes, and snippets.

@MahraibFatima
Created December 24, 2024 17:47
Show Gist options
  • Save MahraibFatima/f7b90cf10ea9b1137434681f6401bed6 to your computer and use it in GitHub Desktop.
Save MahraibFatima/f7b90cf10ea9b1137434681f6401bed6 to your computer and use it in GitHub Desktop.
Safe examples: "#####", "## ####", "# ##", "### #", " ####" Unsafe examples: "# # ###", " # ##", " ## ## ", "# #### #"
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
bool isValidBridge( string s) {
char pre = '#';
int gap_cnt= 0, curr_gap= 0;
for(int i= 0; i<s.length(); ++i) {
if(s[i] == ' ') curr_gap++;
if(pre != s[i] && s[i] == ' ') gap_cnt++;
pre = s[i];
if(gap_cnt>1 || curr_gap>2) return false;
}
return (gap_cnt <= 1 && curr_gap <= 2);
}
int main() {
// Write C++ code here
string str= "# # ";
if(isValidBridge(str))
cout << "True";
else
cout << "False";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment