`
leonzhx
  • 浏览: 793578 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Question 2a String Containing

阅读更多

After reading the suggested solutions of the problem , there is a very interesting solution: use a different prime number to indicate a different alphabet, go through the first string, multiply all the prime number you meet. Then go through the second string, try to divde the product you got for the first string by each prime number you meet, if the remainder is non-zero then return false otherwise return true. I should say it's a creative solution but not a faster solution because big integer multiplication and division is slow. And it's symptotic bound is not O(m+n) because the basic operation (character array access) is no logner the most frequent and time consuming operation.

 

Another thing mentioned by the author with the HashMap/HashSet solution is he suggested going through the shorter string first and then the longer string. In that case , we need to check whether the HashMap/HashSet is empty for every character we traverse in longer string so that we can stop early which I think is time consuming.

0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics