keon / algorithms

Minimal examples of data structures and algorithms in Python
MIT License
23.76k stars 4.59k forks source link

Added a new algorithm within strings module #890

Open Thejas-1 opened 1 year ago

Thejas-1 commented 1 year ago

So I came across this interesting problem that requires us to swap just one character amongst the two strings that are given to us. Once the swap is complete, the strings should contain the same number of distinct characters (The characters need not be the same in the two strings). The function should return True if it is possible to make such a swap and False otherwise. For Example, if we are given two strings 'ddef' and 'dde', we can swap characters f from string 1 and d from string 2. Hence, our final strings will be 'dded' and 'fde', which will contain three distinct characters each (the duplicate characters count as 1) and the algorithm should return True. On the other hand, if we are given two strings 'cd' and 'e', irrespective of swapping characters c and d from string 1 with the character e in string 2, we will always have two distinct characters in string 1 and 1 character in string 2. Hence, the algorithm returns False.