Check if two given strings are ispmorphic
Problem statement:-
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
Example 1:
Input: s = "egg", t = "add" Output: true
Example 2:
Input: s = "foo", t = "bar" Output: false
Example 3:
Input: s = "paper", t = "title" Output: true
Note:
You may assume both s and t have the same length.
Algorithm for the same in Javascript:-
- Prepare a map of characters and the respective indices on which they occur.
- If the string lengths don't match or the number of keys in both the maps don't match return false.
- Sum the frequencies of characters by looping through the respective maps and also concatenate the frequencies of each of the characters in both strings resulting in strings of character frequencies occurring in order.
- Compare strings containing the sum of the frequencies and the character frequencies and return true if either of these match
Code written in Javascript below:-
[cc lang = "javascript" lines="-1" tab_size="4" width="100%"]
/**
@param {string} s
@param {string} t
@return {boolean}
*/
var isIsomorphic = function(s, t) {
var patternMap = {}, strMap = {};
var strArr = t.split('');
var patOccurance = '', strOccurance = '';
var patOccuranceVal = '', strOccuranceVal = '';
if (strArr.length !== s.length) return false;
for (var i = 0; i < s.length; i++) {
if (typeof patternMap[s[i]] === 'undefined')
patternMap[s[i]] = [];
if (typeof strMap[strArr[i]] === 'undefined')
strMap[strArr[i]] = [];
patternMap[s[i]].push(i);
strMap[strArr[i]] .push(i);
}
if (Object.keys(patternMap).length !== Object.keys(strMap).length) return false;
for (var key in patternMap) {
patOccurance += patternMap[key].join('').length;
patOccuranceVal += patternMap[key].join('');
}
for (var skey in strMap) {
strOccurance += strMap[skey].join('').length;
strOccuranceVal += strMap[skey].join('');
}
// console.log(patOccurance, patOccuranceVal, strOccurance, strOccuranceVal);
if (patOccuranceVal.length !== patOccurance.length) {
return (patOccuranceVal === strOccuranceVal);
}
else
return (patOccurance === strOccurance) || (patOccuranceVal === strOccuranceVal);
};
[/cc]
Posted from my blog with SteemPress : https://www.golibrary.co/check-if-two-given-strings-are-ispmorphic/
Leave Check if two given strings are ispmorphic to:
Read more #steempress posts
Best Posts From golibrary
We have not curated any of golibrary's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From golibrary
- Algorithm to find prime factors of very big numbers
- How to create a VM In Microsoft Azure Cloud
- Algorithm to solve linear equation in one variable
- Remove duplicates from sorted array in place
- Finding maximum number of fruits that can be kept in one basket
- Length of smallest subarray with a sum greater than or equal to S
- Finding max sum of subarray with a given length in an array
- Puppet Basic Concepts
- History of Unix and Linux
- Optimizing hierarchical SQL queries