helo. I’m trying to see if strings have the same char in them. I’ve tried but it didn’t work that well (it said it did have it when it shouldn’t). Here’s the code I tried doing it with, but if someone sees the problem, please comment.
void Update()
{
char[] first = curPressedPattern.ToCharArray();
char[] second = pattern.ToCharArray();
if (isSame) {
for (int i = 0; i < first.Length; i++)
{
Debug.Log("Passed first");
char letter = first[i];
if (letter == second[i]) {
Debug.Log("Passed second");
value++;
}
}
if (value >= maxClicks) {
Debug.Log("Passed third");
isSame = true;
}
Ok, thanks for you quick reply. I’m trying to find out if the two strings contain all the same characters in it. So like an example would be like “abc” and “cba”, both of them have the same characters in them but in different orders. Thanks again for your quick reply!
Besides just being easier to write, this is a better solution than looping through the contents of the strings, because it will automagically handle multiple instances of the same characters without any issue.
For example, if you compare akasd and kdaas, both should resolve to aadks and use == to compare. If you were to brute force iterate the contents of the strings through, you’d have to worry about comparisons like akasd and kdsa, because if you’re not careful you might get a false positive result (both containing all the same letters, just in different quantity, but you will find an instance of each letter in the other string though).
You could count the number of times each letter of the alphabet appears in each string, the compare the result. This should give you a O(n) solution that scales well with large strings, and shouldn’t need to generate any garbage.