Home >Web Front-end >JS Tutorial >JavaScript implements finding strings arranged in different orders in an array_javascript tips

JavaScript implements finding strings arranged in different orders in an array_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:35:151271browse

Requirement description: Find the array elements of a set of strings arranged in different orders from a set of arrays. Suppose there is such an array:

Copy code The code is as follows:

[ 'abcd', 'hello', 'bdca', 'olleh', 'cadb', 'nba', 'abn', 'abc' ]

The result to be found is:

Copy code The code is as follows:

[ 'abcd', 'bdca', 'cadb' ]

The key point here is to determine whether a set of strings only has different order of characters. As long as the entire key point is solved, everything else will be easy to handle.

Method 1:

Copy code The code is as follows:

var stringClassify = function( arr ){
var arrLength = arr.length,
         obj = {},
i = 0,
num, item, name, firstItem, strLength;

for( ; i < arrLength; i ){
Item = arr[i];
         strLength = item.length;
         num = 0;

                         // Convert single characters to Unicode encoding
// Calculate the sum of codes
for( j = 0; j < strLength; j ){
              num = item.charCodeAt( j );
                                                                  
If( !firstItem ){
                1stItem = item;
               obj[num].push(item);
}
// Check whether the first character of the string to be added is
// Appear in another string to avoid the following situation
// ['ad', 'da', 'bc']
           else if( ~firstItem.indexOf(item.charAt(0)) ){
               obj[num].push(item);
}
}

for( name in obj ){
console.log( obj[name] );
}
};

Method 1 traverses each character in the string, then converts the single character into Unicode encoding, and calculates the sum of the encodings. The encoding sum of abcd and bdca will be consistent. Finally, use the encoding and the key as the object to save the encoding and consistent string.

Method 1 It should be noted that the Unicode encoding of the strings "ad" and "bc" is the same. At this time, an additional judgment is needed to detect whether the first character in any string appears in the other string. It only needs to appear in a string.

Method 2:

Copy code The code is as follows:

var stringClassify = function(){
var arrLength = arr.length,
         obj = {},
i = 0,
num, item, name, strArr, newStr;

for( ; i < arrLength; i ){
Item = arr[i];

         strArr = arr[i].split( '' );
        strArr.sort();
newStr = strArr.join( '' );

If( !obj[newStr] ){
                obj[ newStr ] = [];
}

          obj[ newStr ].push( item );
}

for( name in obj ){
console.log( obj[name] );
}
};

Method 2 is to convert the string into an array and then sort the array. abcd and bdca will become abcd after sorting. Use the sorted string as the key of the object to save the strings with the same sorting. .

In fact, the principle of both methods is to convert characters into Unicode encoding, but method 1 is an explicit conversion, while the sort used in method 2 will be converted implicitly.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn