I have two strings, and I need to know if one is contained in the other, completely ignoring the order of the characters of both of them.
Example:
string container = "WWGAAFWW";
string element = "WA";
Debug.Log(container.Contains(element));
This gives false, but I need it to be true, because W and A are contained in the container string. I know there is a method to sort lists, but even then, container would become AAFGWWWW and comparing them would still give false. Both string could be longer than this example (but not by much, i think). After having checked that, if the second string is contained (the way I intend) I would also need to remove it from the first one, so in the example, I want the end result to be WGAFWWF. I can't think of a simple way to do this, and I couldn't find any example on how to. Any idea?
CodePudding user response:
I am not sure whether it is what you want
After this function, container will remove the element
static bool CCstring(ref string container, string element)
{
string temp = container;
foreach (char c in element)
{
if (!container.Contains(c.ToString()))
{
container = temp;
return false;
}
else
{
container = container.Remove(container.IndexOf(c), 1);
}
}
return true;
}
CodePudding user response:
you could check this easily using a loop. It's a simple way of doing time complexity O(M*N), because it's checking each char of one string into another string.
private static bool Contains(string container, string element)
{
foreach (char ch in element)
{
if (container.Contains(ch) == false)
{
return false;
}
}
return true;
}