Câu hỏi phỏng vấn Javascript
Câu hỏi

Check if a given st...

Câu trả lời

To check if a given string is isomorphic in JavaScript, you would need to write a function that compares two strings and determines if they are isomorphic. Two strings are considered isomorphic if there is a one-to-one mapping between each character of the first string and each character of the second string, and all occurrences of a character in the first string map to the same character in the second string.

Here is a JavaScript function based on the information provided in the search results:

javascript Copy
function isIsomorphic(str1, str2) {
    if (str1.length !== str2.length) {
        return false;
    }

    const mapping = {};
    const mappedValues = new Set();

    for (let i = 0; i < str1.length; i++) {
        const char1 = str1[i];
        const char2 = str2[i];

        // If char1 has not been seen before
        if (!mapping[char1]) {
            // If char2 is already mapped, return false
            if (mappedValues.has(char2)) {
                return false;
            }
            // Map char1 to char2 and add char2 to the set of mapped values
            mapping[char1] = char2;
            mappedValues.add(char2);
        } else {
            // If char1 is already mapped to a different char, return false
            if (mapping[char1] !== char2) {
                return false;
            }
        }
    }

    return true;
}

This function first checks if the two strings have the same length. If the...

senior

senior

Gợi ý câu hỏi phỏng vấn

junior

What does use strict do?

senior

When would you use import * as X from 'X' ?

senior

What's the difference between a variable that is: null , undefined or undeclared? How would you go about checking for any of these states?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào