streak in a character array
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Elena
el 28 de Mzo. de 2022
Comentada: Elena
el 28 de Mzo. de 2022
how might i find the max streak number for a character array?
ex: something like 'aaaaBBc'
would return 4 because thats the longest streak
I have this and it only works for streaks with capital letters for some reason
cArr = 'aaaaBBc'
if ischar(cArr) == 1 && isempty(cArr) == 0
for i = 2:length(cArr)
if cArr(i) == cArr(i-1)
streak = streak + 1;
else
if streak > maxStreak
maxStreak = streak;
end
streak = 1;
end
end
res = maxStreak;
0 comentarios
Respuesta aceptada
Simon Chan
el 28 de Mzo. de 2022
Sorry that I overlooked the question in my previous answer, here is the updated one:
cArr = 'aaaaBBc';
pos = find(diff([0,diff(cArr)==0,0])==1);
neg = find(diff([0,diff(cArr)==0,0])==-1);
if isempty(neg-pos)
maxStreak = 1
else
maxStreak = max(neg-pos)+1
end
Más respuestas (1)
Mahmoud Ashraf
el 28 de Mzo. de 2022
Editada: Walter Roberson
el 28 de Mzo. de 2022
cArr = 'aaaaBBc';
streak=0;
maxStreak=0;
if ischar(cArr) == 1 && isempty(cArr) == 0
for i = 2:length(cArr)
if cArr(i) == cArr(i-1)
streak = streak + 1;
else
if streak > maxStreak
maxStreak = streak;
end
streak = 1;
end
end
res = maxStreak;
end
0 comentarios
Ver también
Categorías
Más información sobre Common Weakness Enumeration (CWE) en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!