Asp.net checks whether two equal-length strings contain identical characters (ignore character order)

Source: Internet
Author: User

For example, "Beijing Welcomes You" and "Beijing Welcomes You" have the following tests, but there is still room for improvement in efficiency. I don't know if there are any other better methods.
In the beginning, we thought that two conditions are required to determine whether the two strings contain identical characters.
1. The two strings are of the same length.
2. Each character in two strings is in the opposite string.
For example, "Beijing Welcomes You" and "Beijing Welcomes You" have the following code:
Copy codeThe Code is as follows:
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;
}

The test passed, but when the two strings contain repeated characters, such as "4455" and "4555", the above program will be powerless, you can only determine whether two strings contain 4 and 5 characters, but cannot determine whether the number of two characters is the same and cannot meet the requirements. Therefore, the requirement for processing the two strings becomes as follows:
1. Any one of the two strings is in the other string.
2. The number of any one character in the two strings is equal to the number of identical characters in the other string.
3. The two strings are of the same length (due to 2, cancellation is allowed .)
The transformed procedure is as follows:
Checks whether two strings contain identical characters.
Copy codeThe Code is as follows:
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 whether two strings contain identical 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;
}
// Obtain the number of characters in the string
Private static int GetSameCharCount (char chara, char [] arrChar)
{
Int count = 0;
Foreach (char a in arrChar)
{
If (chara =)
{
Count ++;
}
}
Return count;
}
}
}

I feel that there is still a better way. I hope my friends will give me some advice.
The above comparison shows whether two equal-length strings contain exactly the same characters (ignoring the Character Sequence). A friend suggested a simpler method and implemented it at noon. Thanks to Paradox.
The requirement is to compare whether the two strings contain exactly the same characters, that is, "I and you" and "you and me" contain the same characters.
The basic logic of this implementation is as follows: Put the two strings in the List separately for A simple loop judgment. If the characters in set A appear in Set B, delete the character A and B synchronously.
As for some friends who say that the method of using the largest public string is LCS and LD algorithm, if you look for it in the matrix, I personally think it is no longer than how much time this province is, try it if you are free.
Copy codeThe Code is as follows:
[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 of the 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 ");
}
Console. Read ();
}
}
}

[/Code]
Author: LeonWeng
Source: http://cnblogs.com/wengyuli

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.