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.
@ 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" :)
Guys can anyone tell what is wrong with this python code for the problem, according to me it is correct.....
lst=[]
a=int(input())
i=0
while i<a:
str1=input()
lst.append(str1)
i=i+1
i=0
for j in lst:
if '010' in j and '101' in j:
print("Good")
else:
print("Bad")
Guys can anyone tell what is wrong with this python code for the problem, according to me it is correct…
lst=[]
a=int(input())
i=0
while i<a:
str1=input()
lst.append(str1)
i=i+1
i=0
for j in lst:
if '010' in j and '101' in j:
print("Good")
else:
print("Bad")
@s_pandey01
It is in the checking stage. if ‘010’ in j and ‘101’ in j:
it should use ‘or’ instead of ‘and’. If ‘and’ is used, then ‘Good’ will only print out when both ‘010’ and ‘101’ are in the line.
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