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

entry

Explain arrays in JavaScript

middle

How to compare two objects in JavaScript?

middle

What is the difference between Anonymous and Named functions?

Bình luận

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

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