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.

你可能感兴趣的:(Prime)