Skip to content

Commit 63b2fa3

Browse files
committed
changed isAnagram algorythm to other logic
this is my first commit to a public repo, so i don't know if i'm doing it right
1 parent 504140e commit 63b2fa3

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

Algorithms/Strings/Permutations.cs

+5-12
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,12 @@ public static bool IsAnargram(string source, string other)
7373
return true;
7474

7575
int len = source.Length;
76-
// Hash set which will contains all the characters present in input source.
77-
var hashSetSourceChars = new HashSet<char>();
78-
var hashSetOtherChars = new HashSet<char>();
79-
for (int i = 0; i < len; i++)
76+
for(int i = 0; i<len;i++)
8077
{
81-
hashSetSourceChars.Add(source[i]);
82-
hashSetOtherChars.Add(other[i]);
83-
}
84-
for (int i = 0; i < len; i++)
85-
{
86-
// Inputs are not Anargram if characers from *other are not present in *source.
87-
if (!hashSetSourceChars.Contains(other[i])) return false;
88-
if (!hashSetOtherChars.Contains(source[i])) return false;
78+
int index = other.IndexOf(source[i]);
79+
if(index==-1)
80+
return false;
81+
other.Remove(index);
8982
}
9083
return true;
9184
}

0 commit comments

Comments
 (0)