Determine if Two Strings are ‘Close’
Today’s question will be Determine if Two Strings are ‘Close’:
The question asks us if after performing certain operations, are the words ‘close’ (can we attain word2 from performing operations on word1). The first operation allows us to swap any two existing characters. If we think about swapping any two characters, X times, that is essentially the same as ordering our string in any way we want. Let’s look, say we had word1 = ‘abc’ and word2 = ‘bca’. In word2 if we swap ‘b’ -> ‘a’, we then have ‘acb’, and then we can swap ‘c’ -> ‘b’, and we end with ‘abc’. So it appears that unlimited swaps is the same as sorting our string. Then the next operation allows us to transform every occurrence of one existing character for another. So if word1 = ‘bba’ and word2 = ‘aab’, we can swap word2 ‘aa’ -> ‘bb’ and ‘b’ -> ‘a’, giving us ‘bba’. IF we are able to swap every occurrence then the particular letter is not important, just the relative character counts. In our example word1 = ‘bba’ has one letter with an occurrence of 2 and one letter with an occurrence of 1, word2 = ‘aab’ has one letter with an occurrence of 2 and one letter with an occurrence of 1. We don't need to keep track of what the particular letter actually is, just that the relative letter occurrences are the same.