检查字符串 Javascript 中的重复字符 [英] Check for repeated characters in a string Javascript

查看:23
本文介绍了检查字符串 Javascript 中的重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在不使用双循环的情况下检查字符串中的重复字符.这可以通过递归来完成吗?

使用双循环的代码示例(根据字符串中是否有重复字符返回真或假):

var charRepeats = function(str) {for(var i = 0; i <= str.length; i++) {for(var j = i+1; j <= str.length; j++) {if(str[j] == str[i]) {返回假;}}}返回真;}

非常感谢!

解决方案

(可以在此答案的末尾找到递归解决方案.)

您可以使用 javascript 内置数组函数 some MDN 一些参考

 var text = "test".split("");text.some(function(v,i,a){返回 a.lastIndexOf(v)!=i;});

<块引用>

回调参数:
v 当前迭代值
i 当前迭代索引
a 当前数组

<块引用>

.split("") 从字符串创建数组
.some(function(v,i,a){ ... }) 遍历数组直到函数返回true,然后立即结束.(不循环遍历整个数组,如果它更早找到匹配)

<块引用>

some 函数的详细信息可以在 这里

测试,包含多个字符串:

var texts = ["test", "rest", "why", "puss"];for(文本中的var idx){var text = texts[idx].split("");document.write(text + " -> " + text.some(function(v,i,a){return a.lastIndexOf(v)!=i;}) +"<br/>");}//在 chrome 46+ 的 win7 上测试

如果需要递归.

递归更新:

//递归函数函数检查字符串(文本,索引){if((text.length - index)==0 ){//停止条件返回假;}别的{返回检查字符串(文本,索引 + 1)||text.substr(0, index).indexOf(text[index])!=-1;}}//要测试的示例数据var texts = ["test", "rest", "why", "puss"];for(文本中的var idx){var txt = 文本 [idx];document.write(txt + " ->" + checkString(txt,0) + "
");}//在 chrome 46+ 的 win7 上测试

I was wondering if there is a way to check for repeated characters in a string without using double loop. Can this be done with recursion?

An example of the code using double loop (return true or false based on if there are repeated characters in a string):

var charRepeats = function(str) {
    for(var i = 0; i <= str.length; i++) {
        for(var j = i+1; j <= str.length; j++) {
            if(str[j] == str[i]) {
                return false;
            }
        }
    }
    return true;
}

Many thanks in advance!

解决方案

(A recursive solution can be found at the end, of this answer.)

You could use javascript builtin Array functions some MDN some reference

 var text = "test".split("");
 text.some(function(v,i,a){
   return a.lastIndexOf(v)!=i;
 });

callback parameters:
v current value of the iteration
i current index of the iteration
a current array

.split("") create an array from a string
.some(function(v,i,a){ ... }) goes through an array until the function returns true, and ends than right away. (doesn't loop through the whole array, if it finds an match earlier)

Details to the some function can be found here

Tests, with several strings:

var texts = ["test", "rest", "why", "puss"];


for(var idx in texts){
    var text = texts[idx].split("");
    document.write(text + " -> " + text.some(function(v,i,a){return a.lastIndexOf(v)!=i;}) +"<br/>");
    
  }
  //tested on win7 in chrome 46+

If recursion is needed.

Update for recursion:

//recursive function
function checkString(text,index){
    if((text.length - index)==0 ){ //stop condition
        return false; 
    }else{
        return checkString(text,index + 1) 
        || text.substr(0, index).indexOf(text[index])!=-1;
    }
}

// example Data to test
var texts = ["test", "rest", "why", "puss"];

for(var idx in texts){
    var txt = texts[idx];
    document.write( txt +  " ->" + checkString(txt,0) + "<br/>");
}
//tested on win7 in chrome 46+

这篇关于检查字符串 Javascript 中的重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆