INNER CODE UNIT · TypeScript

calculateScore

akirarika/milkio · packages/cookbook/src/index.ts:137

    const calculateScore = (input: string, target: string) => {
      let maxContiguous = 0;
      let currentContiguous = 0;
      let firstMatchIndex = -1;

      let inputIndex = 0;
      for (let i = 0; i < target.length; i++) {
        if (target[i] === input[inputIndex]) {
          if (firstMatchIndex === -1) firstMatchIndex = i;
          currentContiguous++;
          inputIndex++;
          maxContiguous = Math.max(maxContiguous, currentContiguous);
        } else {
          currentContiguous = 0;
        }
      }

      return { maxContiguous, firstMatchIndex };

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…