Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue #11 #149

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions Algorithms/Strings/Permutations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,13 @@ public static bool IsAnargram(string source, string other)
return true;

int len = source.Length;
// Hash set which will contains all the characters present in input source.
var hashSetSourceChars = new HashSet<char>();
var hashSetOtherChars = new HashSet<char>();
for (int i = 0; i < len; i++)
//loops and removes chars from other string so they wouldn't count the same char twice
for(int i = 0; i<len;i++)
{
hashSetSourceChars.Add(source[i]);
hashSetOtherChars.Add(other[i]);
}
for (int i = 0; i < len; i++)
{
// Inputs are not Anargram if characers from *other are not present in *source.
if (!hashSetSourceChars.Contains(other[i])) return false;
if (!hashSetOtherChars.Contains(source[i])) return false;
int index = other.IndexOf(source[i]);
if(index==-1)
return false;
other.Remove(index);
Copy link

@dmitryvhf dmitryvhf Sep 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutly critical code!

  1. Remove(index) - remove From to End. You are drop all symbols from right.
  2. String immunable. "other" object it not changed.
    Example: other = other.Remove(index, 1);
    But this bad too - immunable string operation = bad performance, memory and etc. problem.

StringPermutationTests discredited. This test case must will be fail.

var one = "dbcdefg"; var two = "dabcgfe"; Assert.True(Permutations.IsAnargram(one, two) == true);

}
return true;
}
Expand Down