Skip to content
中文

Implementation of common prefix comparison ​

The policy implementation follows.

ts
export function commonPrefixLength(left: string, right: string): number {
  let low = 0;
  let high = Math.min(left.length, right.length);
  while (low < high) {
    const mid = Math.ceil((low + high) / 2);
    if (left.slice(0, mid) === right.slice(0, mid)) low = mid;
    else high = mid - 1;
  }
  return low;
}

API reference, usage examples and developer guides.