Skip to main content
Code Review

Return to Revisions

2 of 2
Commonmark migration

Generic pattern argument but hardcoded assumptions

It's nice that you added a generic pattern argument, but your code depends on the pattern being "010" here:

 if (j == patternLen) {
 count++;
 chars[i+2] = '1';
 }

Here, you have hardcoded both a 2 and a '1'. Your program would crash if the pattern were shorter than 3 characters and if i were the last index of the array. Also, if the pattern were "101", setting the last character to '1' would not accomplish anything.

It would be better to just skip the rest of the pattern:

 if (j == patternLen) {
 count++;
 i += patternLen - 1;
 }
JS1
  • 28.9k
  • 3
  • 41
  • 83
default

AltStyle によって変換されたページ (->オリジナル) /