Skip to content

Latest commit

 

History

History
83 lines (76 loc) · 1.66 KB

2021-07-28_how-to-find-the-longest-substring-that-consists-of-the-same-char.md

File metadata and controls

83 lines (76 loc) · 1.66 KB
date title
2021-07-28
Фильтр "Сегодня" для Gmail

Как найти самую длинную подстроку, содержащую один и тот же символ

Через split()

{
  const out = 'asdfsdfffff  ffdsafrewtwwwwwt'.split('').reduce(
    (r, s, i, a) => {
      if (s === a[i - 1]) {
        r.data[r.data.length - 1] += 1;
      } else {
        if (
          r.data[r.data.length - 1] &&
          r.max[0]?.length < r.data[r.data.length - 1]
        ) {
          const item = {
            symbol: a[i - 1],
            length: r.data[r.data.length - 1],
          };
          r.max = [item];
        } else if (
          r.data[r.data.length - 1] &&
          r.max[0]?.length === r.data[r.data.length - 1]
        ) {
          const item = {
            symbol: a[i - 1],
            length: r.data[r.data.length - 1],
          };
          r.max.push(item);
        }
        r.data.push(1);
      }
      return r;
    },
    {
      data: [],
      max: [
        {
          symbol: undefined,
          length: 0,
        },
      ],
    }
  );
  console.log(JSON.stringify(out.max, null, '  '));
}

Через подстроку регулярного выражения

{
  const out = 'asdfsdfffff  ffdsafrewtwwwwwt'
    .match(/(.+?)\1*/g)
    .map((item) => ({
      symbol: item[0],
      length: item.length,
    }))
    .sort((a, b) => b.length - a.length)
    .filter((item, _, a) => item.length === a[0].length);
  console.log(JSON.stringify(out, null, '  '));
}

Результат

[
  {
    "symbol": "f",
    "length": 5
  },
  {
    "symbol": "w",
    "length": 5
  }
]