asp.net compare two equal length strings that contain exactly the same characters (ignore the character order)

  • 2020-05-10 17:58:45
  • OfStack

For example, "Beijing welcomes you" and "welcome to Beijing", so the following tests were conducted, but it was found that there should be room for improvement in efficiency. I wonder if there is any other better way.
The first thought was that there are two conditions to determine whether the characters in two strings contain exactly the same characters
1, the two strings are of the same length
2, each character in two strings is in the string of the other
For example: "welcome to Beijing" and "welcome to Beijing", so we have the following code:
 
private static bool CompareStringByChar(string strA,string strB) 
{ 
bool IsEqual = true; 
char[] arrA = strA.ToCharArray(); 
char[] arrB = strB.ToCharArray(); 
foreach (char chara in arrA) 
{ 
if (!strB.Contains(chara)) 
{ 
IsEqual = false; 
} 
} 
foreach (char charb in arrB) 
{ 
if (!strA.Contains(charb)) 
{ 
IsEqual = false; 
} 
} 
return IsEqual; 
} 

Test pass, but when there are repeated characters in two strings, such as: "4455" and "4555", then the above procedure is powerless, can only judge two strings contain 4 and 5, but unable to judge whether the number of the two characters 1, unable to meet the requirements, so deal with the needs of the two strings and become like this:
1, any character in two strings is in each other's string
2, the number of any 1 character in two strings is equal to the number of the same character in the other string
3, the two strings have the same length (cancelable because of 2).
The procedure after modification is as follows:
Compare two strings to see if they contain exactly the same characters
 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
namespace StringCompare 
{ 
class Program 
{ 
static void Main(string[] args) 
{ 
string a = "4555"; 
string b = "5544"; 
Console.WriteLine(CompareStringByChar(a,b)); 
Console.Read(); 
} 
// Compare two strings to see if they contain exactly the same characters  
private static bool CompareStringByChar(string strA,string strB) 
{ 
bool IsEqual = true; 
char[] arrA = strA.ToCharArray(); 
char[] arrB = strB.ToCharArray(); 
foreach (char chara in arrA) 
{ 
if (!strB.Contains(chara)) 
{ 
IsEqual = false; 
} 
else 
{ 
if(GetSameCharCount(chara,arrA)!=GetSameCharCount(chara,arrB)) 
{ 
IsEqual = false; 
} 
} 
} 
foreach (char charb in arrB) 
{ 
if (!strA.Contains(charb)) 
{ 
IsEqual = false; 
} 
else 
{ 
if (GetSameCharCount(charb, arrA) != GetSameCharCount(charb, arrB)) 
{ 
IsEqual = false; 
} 
} 
} 
return IsEqual; 
} 
   // Gets the number of characters in a string  
private static int GetSameCharCount(char chara,char[] arrChar) 
{ 
int count = 0; 
foreach(char a in arrChar) 
{ 
if(chara==a) 
{ 
count++; 
} 
} 
return count; 
} 
} 
} 

Feel there is a better way, throw a brick to attract jade, hope friends not stingy grant.
The above comparison of two equal length strings containing the same characters (ignore the character order) a friend said a simpler way to achieve 1 at noon. Thank you for your Paradox.
The requirement is to compare whether two strings contain exactly the same characters, that is, "I and you" and "you and I" have the same characters.
The basic logic of this implementation goes like this: put two strings into List, do a simple loop judgment, and if a character in the A collection appears in B, delete the character in A and B simultaneously.
As for the maximum public string method, LCS and LD algorithm, if I look in the matrix, I don't think it will save me much time.
 
[code] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Collections; 
namespace StringCompare 
{ 
class Program 
{ 
static void Main(string[] args) 
{ 
string strA = " People's Republic of China "; 
string strB = " People's Republic of China "; 
List<char> listA = strA.ToList(); 
List<char> listB = strB.ToList(); 
for (int i = 0; i < listA.Count;i++ ) 
{ 
for (int j = 0; j < listB.Count;j++ ) 
{ 
if (listA[i].ToString() == listB[j].ToString()) 
{ 
listA.RemoveAt(i); 
listB.RemoveAt(j); 
i--; 
j--; 
break; 
} 
} 
} 
if (listA.Count == 0 && listB.Count == 0) 
{ 
Console.WriteLine(" equal "); 
} 
else 
{ 
Console.WriteLine(" Not equal to the "); 
} 
Console.Read(); 
} 
} 
} 

[/code]
Author: LeonWeng
Reference: http: / / cnblogs com/wengyuli

Related articles: