简体   繁体   English

检查 String 在 javascript 中是否有连续或重复的字符(下划线)

[英]Check if String has sequential or repeated characters in javascript (underscore)

I have code that I am trying to refactor.我有我正在尝试重构的代码。 Im new to javascript so Im tring to make more readable code using functions in libraries like underscore.我是 javascript 的新手,所以我想使用下划线等库中的函数来制作更具可读性的代码。 The function below can detect when string contains 3 or more ordered characters such as (234, efg, LmN) and when string contains 3 or more repeated (lll, 444, MMm, @@@)下面的 function 可以检测字符串何时包含 3 个或更多有序字符,例如 (234, efg, LmN) 以及何时字符串包含 3 个或更多重复 (lll, 444, MMm, @@@)

const input = "Dfdf123125";

    const myStr = input.toLowerCase();

    const n = 3;

    let isRepeating = false;

    let isSequential = false;

    for (let i = 0; i < myStr.length; i++) {

      if (i + (n - 1) <= myStr.length) {
 
        let isRepeatingTemp = false;

        let isSequentialTemp = false;

        for (let j = i; j < i + n; j++) {

           (myStr.charCodeAt(i) === myStr.charCodeAt(j)) ? isRepeatingTemp = true: isRepeatingTemp = false;

           (myStr.charCodeAt(i) === myStr.charCodeAt(j) - (n - 1)) ? isSequentialTemp = true : isSequentialTemp = false;

        }

        if (isRepeatingTemp) isRepeating = true;

        if (isSequentialTemp) isSequential = true;

      }

    }

Im trying to to see if I can optimize this and make it more readable with underscore and/or even make time/space complexity better.我试图看看我是否可以优化它并使用下划线使其更具可读性和/或什至使时间/空间复杂性更好。 I know this can also be done with regx but im trying to get it done without it.我知道这也可以用 regx 来完成,但我试图在没有它的情况下完成它。

Instead of the inner for loop, I chunked the string to n using Array.prototype.slice() to see ahead n characters.我没有使用内部for循环,而是使用Array.prototype.slice()将字符串分块为n以提前查看n字符。 I used Array.prototype.indexOf() to find if it's sequential based off the abc and num constants( ref ).我使用Array.prototype.indexOf()根据abcnum常量( ref )来查找它是否是顺序的。 To see if it's repeating, I used Array.prototype.every() that loops through the chunk and check if they're similar and return a boolean based on the expression.为了查看它是否重复,我使用Array.prototype.every()循环遍历块并检查它们是否相似并根据表达式返回 boolean。

The result gives the output of each instance found, and if it was sequential or repeating.结果给出了找到的每个实例的 output,以及它是连续的还是重复的。

const input = "Dfdf123125";

function RepSeq(str, n) {
  var rep = false;
  var seq = false;
  var result = [];
  
  const num = '0123456789';
  const abc = 'abcdefghijklmnopqrstuvqxyz';
  
  if (str.length < n) return false;
  
  for (var i = 0; i < str.length; i++) {
    if (i + n <= str.length) {
      var chunk = str.slice(i, i + n);
      var seqABC = abc.indexOf(chunk) > -1;
      var seq123 = num.indexOf(chunk) > -1;
      
      if (seq123 || seqABC) {
        seq = true;
        result.push(chunk);
      }
      
      if ([...chunk].every(v => v.toLowerCase() === chunk[0].toLowerCase())) {
        rep = true;
        result.push(chunk);
      }
    } else break;
  }
  
  return {
    repetition: rep,
    sequential: seq,
    out: result
  };
}


console.log(RepSeq(input, 3));

// Output:
// {
//   out: ["123"],
//   repetition: false,
//   sequential: true
// }

With this method, we're peeking at the string one block( i + n ) at a time.使用这种方法,我们一次只查看一个块 ( i + n ) 的字符串。 Ex( n=3 ):例( n=3 ):

 1. [Dfd]f123125
 2. D[fdf]123125
 3. Df[df1]23125
 4. Dfd[f12]3125
 5. Dfdf[123]125 - Sequential!
 6. Dfdf1[231]25
 7. Dfdf12[312]5
 8. Dfdf123[125]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM