Determine whether the given binary string contains the substring (consecutive) “010” or “101”.
EXPLANATION:
You can use a loop and condition clauses to check directly. Also, you can use some built-in functions to solve this problem too.
For example, C++, we can use
int position = strstr(s, "010") - s;
to get the first occurrence of “010” and check whether this position is in range. Or, if string is used in C++, then
int position = s.find("010");
will work for string.
Similarly, Java, Python, etc… a lot of languages have such functions. Post your accepted solution and let’s find which one is the shortest! I think it will be interesting
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here.
Tester’s solution can be found here.
The problem with this check is that it doesn’t receive input and produce output, instead, it can be used in an interactive fashion, but, I’m loving Haskell so far
don’t be sad, I can write a book about stupid bugs I did in contests
My advice here is, do not use some side effects (like if I scanned whole string), use additional boolean flag that is set to false at the beginning and when you really find what you are looking for, set it to true.
what is wrong with this… #include <stdio.h> #include <string.h>
int main(void)
{
int t;
char a , b , c ,d ,count ;
scanf("%d" , &t);
while(t–)
{
count = 0;
a = getchar();
b = getchar();
c = getchar();
while( (d = getchar()) != ‘\n’)
( (a==‘0’ && b==‘1’ && c==‘0’) || (a == ‘1’ && b==‘0’ && c==‘1’)) ? ( (count++) , (a = b) , ( b = c) , (c = d )): ((a = b) , ( b = c), (c = d ));
(count > 0)?(printf(“Good\n”)):(printf(“Bad\n”));
}
return 0;
}
@ rd13123013
When 010 or 101 comes at the last of the string …then d==’\n’ .Therefore ,it doesn’t enters into While loop…so the count doesn’t increasing …and there lies the bug.
So , you can overcome this by using "do() ...while()..loop" :)