aalhour / aalhour/C-Sharp-Algorithms

IsAnagram bug (per element counts)

未關閉 適合新手
#153 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
C#
星號
6.2k
分支
1.4k
PR 合併指標
30 天內沒有已合併 PR

描述

### 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._

貢獻指南

開啟貢獻指南

研究方向

問題出在 Permutations.IsAnagram 方法中。查看單元測試以了解目前的行為。這個錯誤在於它沒有檢查每個元素的數量。修正方式是更新演算法,計算每個字元的出現次數。提供的外部連結展示了修正後的實作。先新增失敗的測試案例,然後修改該方法使其通過測試。

由索引模型根據 Issue 內容生成。

評估

領域
testing-qa
Issue 類型
缺陷
難度
2/5
預估耗時
1-3 小時
活躍度
停滯
描述清晰度
描述清楚
新手友好度
70/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。