Category: algorithms

Codeforces Problem: Azamon Web Services: Solution in Python

Problem Given two string, find if using max one swap of characters, the first string is lexicographically smaller than the other string. Solution The logic is as follows: Create an index map of each character in the string A. Compare String A & String B, character-by-character. If characters are the same, move to the next character. If char ‘c’ in…

How to Select a Random Key from a Hash Map in Constant Time?

Premise A hashmap has a time complexity of O(1) for all operations. Problem You have to find a constant time method to uniformly random selection of a key in a Hash map. Assumptions The map can grow to memory size. You can use any readymade hash map. Solution I’m discussing the pseudo code for a Python solution. All operations work…