[英]Given two strings, check if they're k-equal, every substring of length k appear on both and has to be max
I'm trying to solve a problem: Given 2 strings, I need to check if they're k-equal, meaning every sub string of length k in str1 appears in str2 and vice versa. 我正在尝试解决一个问题:给定2个字符串,我需要检查它们是否等于k,这意味着str1中长度为k的每个子字符串都出现在str2中,反之亦然。 The number of occurnces of the sub-strings does not have to be equal though.
但是,子字符串的出现次数不必相等。 we can assume that the string are zero equal by default.
我们可以假设默认情况下字符串为零。 Lastly, the k has to be the highest number we can find.
最后,k必须是我们可以找到的最大数。 thus if there are sub-strings of length 1 and length 2 that appear on both strings, we should return just the strings with length 2.
因此,如果两个字符串上都出现了长度为1和长度为2的子字符串,我们应该只返回长度为2的字符串。
How can I approach this with Collections? 如何使用收藏夹解决这个问题? for example with HashMap.
例如使用HashMap。
Hash all substring of str1 in a HashSet, lets say hash1. 将str1的所有子字符串哈希到一个HashSet中,比方说hash1。 Now generate substrings of str2 and check if it exists in hash1 and also maintain a max variable .
现在生成str2的子字符串,并检查它是否存在于hash1中,并还维护一个max变量。 I thinks this should work but its kind of bruteforce approach.
我认为这应该有效,但是它是一种蛮力的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.