Can you help me this question using matlab.Given a string s = \'0111100100000001
ID: 3694163 • Letter: C
Question
Can you help me this question using matlab.Given a string s = '011110010000000100010111', the length of the longest substring of s which contains consecutive ‘1's would be 4. Write a function named longest_one , which accepts one input string consisting only of ‘0’ and ‘1’ characters. The function should return the size of the longest substring of consecutive ‘1’s. You are required to use the programming method (loops, conditional statements). The function should return the desired output regardless of the input string size . Here's an example of a function call:
longest_one('011110010000000100010111')
ans = 4
Explanation / Answer
Program:
s = '011110010000000100010111'
lenmax = 1;
len = 1;
for n = 2:numel(s)
if s(n) == s(n-1) && s(n-1) == '1';
len = len+1;
else
if len > lenmax
lenmax = len;
end
len = 1;
end
end
sprintf('Ans = %d', lenmax)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.