Skip to content

Latest commit

 

History

History
33 lines (25 loc) · 1.18 KB

File metadata and controls

33 lines (25 loc) · 1.18 KB

Array

Screen Shot 2023-01-09 at 11 57 21 PM

Hashmap

The time complexity of this code is O(n), where n is the length of the input string s. This is because the code iterates through each character of the string s exactly once and performs a constant amount of work for each character, including hash table lookups, insertion, and comparison.

The space complexity of this code is O(n), where n is the length of the input string s. This is because in the worst case, the hash table map could contain one entry for each character in s, requiring O(n) space. Additionally, the set [...map.values()] also requires O(n) space in the worst case.

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isIsomorphic = function(s, t) {
    let map = new Map();

    for(let i = 0; i < s.length; i++) {
        if(map.has(s[i])) {
            if(map.get(s[i]) !== t[i]) {
                return false;
            }
        } else {
            map.set(s[i], t[i]);
        }
    }
    
    return new Set([...map.values()]).size === map.size;
};