是否存在可以忽略此字符串中字符顺序的字符串散列 [英] Does a string hash exist which can ignore the order of chars in this string

查看:95
本文介绍了是否存在可以忽略此字符串中字符顺序的字符串散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在可以忽略字符串中字符顺序的字符串散列?例如,helloword和wordhello可以映射到同一个桶中。

解决方案

ac和bb生成相同的散列值,则可以将这些字符的值相加。 (a + b + c等于a + c + b)。

如果你想减少这种哈希码冲突的可能性,那么你可以乘以字符的值一起。 (a * b * c也等于a * c * b。)



如果这还不够好,那么您可以先排序字符串中的所有字符使用您正在使用的任何语言提供给您的默认字符串散列函数。 (因此,在散列之前,helloword和wordhello都会变成dehlloorw,从而生成相同的哈希码。)


Does a string hash exist which can ignore the order of chars in this string? Eg."helloword" and "wordhello" can map into the same bucket.

解决方案

If you don't mind strings like "ac" and "bb" generating the same hash value, you can add the values of the characters together. (a + b + c is equal to a + c + b.)

If you want to reduce the possibility of hash code collisions of that kind, then you can multiply the values of the characters together. (a * b * c is also equal to a * c * b.)

If that's not good enough either, then you can sort all the characters in the string before applying the default string hashing function offered to you by whatever language it is that you are using. (So, both "helloword" ad "wordhello" would become "dehlloorw" before hashing, thus generating the same hash code.)

这篇关于是否存在可以忽略此字符串中字符顺序的字符串散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆