Coding Challenges
For this week I did 2 more coding problems. The first one I did was more challenging and its called isIsomorphic. Lets say we have 2 strings and a holds the value of the first string and b holds the value of the second string. Two strings are isomorphic only if you can replace all the characters in a with all the characters in b. That means that all the occurrences of a character in a string a must map directly back to another character in the string b. They must back and forth together in order for it to be isomorphic. For example the string abb is isomorphic with the string cdd. That is because for every a character you can replace it with the character c. Also every b character you can replace it with the letter d. In our problem we can also assume that both strings that are given will always be of equal length.
When I first started the problem right away I thought to create a dictionary. We could loop through the strings and map every character in string a with string b. Once we have our keys and values we could just check every index after that if the next character is already in our dictionary. We could check this for true and false. However after coding that solution I came to a problem. If we map every character in word a with b. What happens if we encounter all different characters in string a but 2 similar characters in string b? For example what if string a holds "badc" and string b holds "baba"? In string a the character b would map to b. The character a would map with a. The character d would then map with b. However b already maps to b. This is where I encountered my problem. I then decided to write 2 dictionaries. One that maps a to b but then we would also map b to a. That way every character matches back and forth.
In my function I initialized 2 dictionaries one to each string. I then created an index that starts at 0. We use a while loop to loop through one of the strings. Since both strings will always be of equal length it does not matter which one we use to define the length of our loop. We then check if the current index of s string is already in our dictionary as a key. If its not there we just add the key however if it is we check if the value in that key equals to the value in the character in the string t. If it doesnt equal then we return false because at that point we broke the rules of isomorphic. We then do the exact same thing in our other dictionary. Now we map it backwards. This prevents the problem I explained before. Now both dictionaries must map back and forth in order for the function to be isomorphic.
The last problem I did is called is subsequence. This one I took a similar approach as our previous problem. If one word is a subsequence of second word the characters in the first word must all appear in the second word in order but not right next to each other. I decided to create a list and loop through both words. I then had an if condtion and check if at every index the characters are equal. If they are we append that character and increment both indexes. If not then we stay at the first words index and increment the second words index. At the end we return the subsequence by converting the list to a string.
Comments
Post a Comment