4 ms·
"Because order should not matter, both arrays are sorted before they are compared." I do not understand this line very well. If order does not matter, why sort
by mobilemidget 7y ago
"Because order should not matter, both arrays are sorted before they are compared."
I do not understand this line very well. If order does not matter, why sort anything?
- GrayShade 7y agoThey order does not matter and can be arbitrary. But when you compare two entries you need to sort them, because the entries are arrays. It's like the trick of converting sttings to lower-case to get an case-insensitive comparison (which is a bad idea in a non-Latin world, but that's another story).
- ignoramous 7y agoConsider character sequences: [n, e, t] and [t, e, n]. If you'd want to know if the lists are equivalent (have the same characters, same number of times), then sorting them would make the comparison straight-forward since order doesn't matter. See: https://leetcode.com/problems/valid-anagram/solution https://leetcode.com/problems/valid-anagram/solution
- mobilemidget 7y agoah yes but of course, probably did not have enough coffee yet when I read it :) thank you But then perhaps it is worth storing it sorted ( or as well if original value is important) and making it even faster?