【问题标题】:How to get longest sequences of equal characters and how many are they if more than one?如何获得最长的相等字符序列,如果超过一个,它们有多少?
【发布时间】:2018-09-03 14:08:58
【问题描述】:

例子:

  • 11ADD = 2 个相等,2 个序列
  • 115222 3个等于,1个序列
  • ABCDEF10 = 1 个相等,8 个序列

结果应该包含

  1. 按顺序重复的最大字符数的长度。
  2. 与最大长度计数匹配的重复字符序列数

这是我正在尝试的,将 dec 转换为 Hex,然后找到相等的 seq。他们有多少个

let n = [1, 26, 221];
let arr = [];
let hex = '';
let maxSequence = 0;
let currentCounter = 0;
let sequences = 0;


arr.forEach(element => {
  hex += element.toString(16).toLocaleUpperCase();
});
let newarr = [];
const hexSplit = [...hex];

hexSplit.forEach((element, x) => {
  if (hexSplit[x + 1] === hexSplit[x]) {
    currentCounter++;
    newarr.push(hexSplit[x])
  }
  if (currentCounter > maxSequence) {
    maxSequence = currentCounter;
    sequences += 1;

  } else if (maxSequence === 0) {
    maxSequence = 1;
    sequences = hexSplit.length;
  }
});

console.log(maxSequence, sequences)

【问题讨论】:

  • 序列将由单个char 组成,或者它们可以是string? ABCABC1 在此字符串中,序列 ABC 重复两次
  • @liam 我不认为这是重复的,因为这里需要两个值(有多少,以及最大长度序列的计数),它不是“发生”,而是顺序发生在这里需要。
  • ABCDEA 的预期结果是什么?

标签: javascript arrays sequence equals


【解决方案1】:

你可以用一个简单的正则表达式得到最长的相等字符序列的列表,按长度降序排序并按第一个(最长)的长度过滤:

function getLongestSeqList(input) {
  return input
          .match(/(.)\1*/g)
          .sort((a,b) => b.length - a.length)
          .filter((a,i,arr) => a.length === arr[0].length)
}

console.log(getLongestSeqList('11ADD')); // ["11", "DD"]
console.log(getLongestSeqList('115222')); // ["222"]
console.log(getLongestSeqList('ABCDEF10')); // ["A", "B", "C", "D", "E", "F", "1", "0"]

【讨论】:

  • 为每个示例添加有效console.log("Count",getLongestSeqList('115222').length,"Len",getLongestSeqList('115222')[0].length);,我可以购买这个作为可能优化的答案,即让该函数返回有效的结果。
【解决方案2】:

    const getLongest = seq => seq.reduce(({
  longest,
  currentItem,
  currentLength,
  items
}, item) => {

  const newCurrentLength = currentItem === item ? currentLength + 1 : 1;
  if (newCurrentLength > longest) {
    // we have a new longest sequence, assign new values
    // to longest and items
    return {
      currentItem: item,
      currentLength: newCurrentLength,
      longest: newCurrentLength, 
      items: 1,
    };
  } else if (newCurrentLength === longest) {
    // we match the previously longest,
    // add the item to the sequence list
    return {
      currentItem: item,
      currentLength: longest,
      longest, 
      items: items + 1,
    };
  } else {
    // this is still a shorter sequence,
    // just update the local state
    return {
      currentItem: item,
      currentLength: newCurrentLength,
      longest,
      items
    }
  }


  return ;

}, {
  longest: 0,
  currentItem: undefined,
  currentLength: 0,
  items: 0
});

const values = ["11ADD", "115222", "ABCDEF10"];

const res = values.map(s => s.split("")).map(getLongest);

console.log(res);

请注意,通过这种组织方式,您可以轻松地将数据添加到结果中,例如最长序列的值是什么,从哪里开始等等...

【讨论】:

  • 对于第一个示例,您有 "currentItem": "D",,但忽略该字符串中的“11” - 也许 currentItem 作为数组更好? (但这并不意味着这个问题在这里没有答案。
  • 结果中的currentItem实际上是Din第一个例子中的最后一项
  • 读取结果:items 是具有较长序列的不同值的数量。 longest 是该序列的长度。其他只是地方状态。当然,您可以用数组替换 items 并在第二个 if 子句中执行 items : [...items, item] 以获取值(以及它们出现的顺序)。
【解决方案3】:

这里的所有功劳都归于这里的其他答案。 https://stackoverflow.com/a/52152057/125981 我只是将其调整为针对一组值返回结果。请投票/接受那个也许。

var sequences = ['11ADD', '115222', 'ABCDEF10'];

function getLongestSeqList(input) {
  return input
    .match(/(.)\1*/g)
    .sort((a, b) => b.length - a.length)
    .filter((a, i, arr) => a.length === arr[0].length)
}

function getValues(s) {
  let seq = getLongestSeqList(s);
  return {
    Count: seq.length,
    Len: seq[0].length
  };
}
sequences.forEach(function(s) {
  console.log(getValues(s));
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2020-04-18
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多