gpt4 book ai didi

javascript - 如何检查字符串是否仅由相同长度的字符组组成?

转载 作者:行者123 更新时间:2023-12-03 12:16:28 24 4
gpt4 key购买 nike

我想识别完全由相同长度的字符组组成的字符串。这些组中的每一个都由至少两个相同的字符组成。所以,这里有一些例子:

aabbcc          true
abbccaa false
xxxrrrruuu false (too many r's)
xxxxxfffff true
aa true (shortest possible positive example)
aabbbbcc true // I added this later to clarify my intention

@ilkkachu:感谢您关于重复相同字符组的评论。我添加了上面的示例。是的,我希望最后一个样本被测试为真:由两个字母组组成的字符串 aa, bb, bb, cc .

有没有一种简单的方法可以使用正则表达式和 JavaScript 对字符串进行条件检查?

我的第一次尝试是做类似的事情
var strarr=['aabbcc','abbccaa','xxxrrrruuu',
'xxxxxfffff','aa','negative'];
var rx=/^((.)\2+)+$/;

console.log(strarr.map(str=>str+': '+!!str.match(rx)).join('\n'));

它确实会查找重复字符组,但尚未注意这些组的长度都相同,如输出所示:
aabbcc: true
abbccaa: false
xxxrrrruuu: true // should be false!
xxxxxfffff: true
aa: true
aabbbbcc: true
negative: false

如何获得检查以查找相同长度的字符组?

最佳答案

要获得相同字符的所有组,有一个简单的正则表达式解决方案:

/(.)\1*/g

只是重复反向引用 \1捕获组 1 中的 Angular 色。

然后只需检查相同字符串数组中是否存在不匹配的长度。

示例片段:

function sameLengthCharGroups(str)
{
if(!str) return false;
let arr = str.match(/(.)\1*/g) //array with same character strings
.map(function(x){return x.length}); //array with lengths
let smallest_length = arr.reduce(function(x,y){return x < y ? x : y});
if(smallest_length === 1) return false;
return arr.some(function(n){return (n % smallest_length) !== 0}) == false;
}

console.log("-- Should be true :");
let arr = ['aabbcc','xxxxxfffff','aa'];
arr.forEach(function(s){console.log(sameLengthCharGroups(s)+' : '+ s)});

console.log("-- Should also be true :");
arr = ['aabbbbcc','224444','444422',
'666666224444666666','666666444422','999999999666666333'];
arr.forEach(function(s){console.log(sameLengthCharGroups(s)+' : '+ s)});

console.log("-- Should be false :");
arr = ['abbcc','xxxrrrruuu','a','ab','',undefined];
arr.forEach(function(s){console.log(sameLengthCharGroups(s)+' : '+ s)});


带有粗箭头的 ECMAScript 6 版本(在 IE 中不起作用)
function sameLengthCharGroups(str)
{
if(!str) return false;
let arr = str.match(/(.)\1*/g)
.map((x) => x.length);
let smallest_length = arr.reduce((x,y) => x < y ? x : y);
if(smallest_length === 1) return false;
return arr.some((n) => (n % smallest_length) !== 0) == false;
}

或者使用 exec 而不是 match,对于大字符串应该更快。
因为它可以在找到不同的长度后立即退出 while 循环。
但这有一个缺点,即在比较它们之前,它无法获得所有长度的最小长度。
因此,以这种方式无法找到末尾具有最小长度的那些。

function sameLengthCharGroups(str)
{
if(!str) return false;
const re = /(.)\1*/g;
let m, smallest_length;
while(m = re.exec(str)){
if(m.index === 0) {smallest_length = m[0].length}
if(smallest_length > m[0].length && smallest_length % m[0].length === 0){smallest_length = m[0].length}
if(m[0].length === 1 ||
// m[0].length !== smallest_length
(m[0].length % smallest_length) !== 0
) return false;
}
return true;
}

console.log("-- Should be true :");
let arr = ['aabbcc','xxxxxfffff','aa'];
arr.forEach(function(s){console.log(sameLengthCharGroups(s)+' : '+ s)});

console.log("-- Should also be true :");
arr = ['aabbbbcc','224444','444422',
'666666224444666666','666666444422','999999999666666333'];
arr.forEach(function(s){console.log(sameLengthCharGroups(s)+' : '+ s)});

console.log("-- Should be false :");
arr = ['abbcc','xxxrrrruuu','a','ab','',undefined];
arr.forEach(function(s){console.log(sameLengthCharGroups(s)+' : '+ s)});

关于javascript - 如何检查字符串是否仅由相同长度的字符组组成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50895803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com