Skip to content
English

公共前缀的比较实现 ​

策略的具体实现如下。

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;
}

接口参考、使用示例与开发指南。