aalhour / aalhour/C-Sharp-Algorithms
IsAnagram bug (per element counts)
- Ngôn ngữ chính
- C#
- Star
- 6.2k
- Fork
- 1.4k
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
### Describe the bug
The `IsAnagram` function is not checking per-element counts. It is only checking if they have the same elements, but not if the count of each element matches.
A more appropriate name for the current logic is something like `ContainsNoDifferingElements` or `IntersectsMatch` rather than `IsAnagram`. I would recommend changing the name or the logic of the method.
> Note: If you aren't going to check per-element counts, then you should also get rid of this check in `IsAnagrams`:
> ```cs
> if (source.Length != other.Length)
> return false;
> ```
> because length doesn't matter if you don't also check per-element counts.
### To Reproduce
Add the following case to the `IsAnagram` unit tests:
```cs
string aab = "aab";
string abb = "abb";
Assert.False(Permutations.IsAnargram(aab, abb));
```
### Expected behavior
Spans of the same length and elements but different per-element counts should not be considered re-orders/anagrams of each other.
### Environment:
_master branch_
### Additional context
I have written my own version of this algorithm in C# _(that fixes this issue)_ if interested here...
> Source Code: https://github.com/ZacharyPatten/Towel/blob/d2660e208ad3a44ab22f192834760c5b93dc82ac/Sources/Towel/Statics-SequenceAnalysis.cs#L1321
> Examples: https://github.com/ZacharyPatten/Towel/blob/d2660e208ad3a44ab22f192834760c5b93dc82ac/Examples/BasicsAndExtensions/Program.cs#L406
> Testing: https://github.com/ZacharyPatten/Towel/blob/d2660e208ad3a44ab22f192834760c5b93dc82ac/Tools/Towel_Testing/Statics.cs#L2086
> _Note: `MapHashLinked` is my version of a `Dictionary` if you look at the source code._
Hướng dẫn đóng góp
Hướng nghiên cứu
Vấn đề nằm trong phương thức Permutations.IsAnagram. Hãy xem các unit test để hiểu hành vi hiện tại. Lỗi là phương thức không kiểm tra số lượng của từng phần tử. Cách sửa là cập nhật thuật toán để đếm số lần xuất hiện của từng ký tự. Liên kết bên ngoài được cung cấp có phần triển khai đã được sửa. Trước tiên, hãy thêm test case bị lỗi, sau đó sửa đổi phương thức để test case đó chạy qua.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Lĩnh vực
- testing-qa
- Loại issue
- Lỗi
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 70/100