DSA GRIND #3

 DSA GRIND #3

By Ashur Baroutta

    This week we solved two problems. The first problem reads as follows : "Given two strings , s and t, return true if "t" is an anagram of "s", and false otherwise". An anagram is a word formed by rearranging the letters of a different word, using the original words letters exactly once (typically). 

    To solve this I started with setting up an if condition to return false for the edge case in which the length of s did not match the length of t, as if the lengths don't match then the words are obviously not anagrams of each other. I then turned both the strings into character arrays and sorted them. After sorting, I made a for loop and checked each index for a character match, as if one failed to match I knew it was not an anagram. The code submission is shown below. I am pleased with the performance.



    The second problem I worked on was a bit more complicated for me though it is built in part on the previous problem. It reads "Given an array of strings "strs", group the anagrams together. You can return the answer in any order.

    So this one was a bit more challenging in that I had to go through the string array and then break down each word in the string array into character arrays and assign value to each character so that I had identifiers to store and track if another word matched. I did this by making the character array size 26 as there are 26 letters in the alphabet.

    The way to go about that was to utilize the combination of lists and hashmaps. We are going to go through each word, assign value to the characters and then store the words with the same character values into the the hashmap as a list, that way we return anagrams grouped with eachother in their own lists. The submission results are as follows. This was super hard and took the bulk of my time, for now im okay with how it ranked.


 

Comments

  1. Ashur, great progress again this week. I'm curious if you can extrapolate and give a real world scenario where solving (one of) these problems will help you down the road in your career. More specifically, can you see where the structure of a command may be applied to different scenarios? I'd love to see a blurb about this in one of your blogs!

    ReplyDelete
  2. This is an interesting specific problem. I never thought of writing a code that involved solving for an anagram (which is a new word for me). I can see this being used for generating a random password or trying to solve a fun worksheet of scrambled words. I feel as though this is something that does not have many applications but is definitely something that you learn to prepare yourself for a build off of this concept in more complicated applicable problems. Maybe this can be used for matrices in the way numbers are arranged.

    ReplyDelete

Post a Comment

Popular posts from this blog

IP #6 : Reversing an Integer

IP#5 : LinkedList Removing Duplicates II