Code and Why

Photo Credit: Christopher Gower
  • About Me

    Hello and welcome to my blog site! My name is Pranav Patil, and currently, I am a Computer Science and Math student at Rutgers University-New Brunswick. I have always enjoyed learning about technology, specifically how computers do the things they do and how they interact with the user, especially as the world is rapidly transforming to a new age of technology. As of right now, I have been learning the programming language Java for 4 years, as well as 3 years of Python. In the future, I hope to be able to expand my horizons and learn even more coding languages. I hope to spread my own perspectives and ideas upon some programming issues in upcoming posts. Many problems I will post will be from the website LeetCode, which is a popular website that has a plethora of problems with varying difficulty, used by many to prepare for interviews. Anyways, that has been more than enough for you guys to read, so get ready, and have fun! 

    Sidenote: I shall be solving most of the problems in Java, however LeetCode allows for the users to choose which language to submit the response in.

  • LeetCode #26 – Remove Duplicates from Sorted Array

    Here is the next problem:

    Once again, we have a problem that has a large description. Not ideal. However, it was labeled ‘Easy’, so I had to give it a go. Though, judging from the likes and dislikes, I thought that this one would be a bit annoying(spoiler alert: it was). Here are some examples of output:

    After you take a look at the explanation, it becomes a lot easier to understand what is going on. At least, it was for me when I was doing this problem. As you may have noticed from the description and the title of the problem, the array is sorted in non-decreasing order, which is what we have to take advantage of here. Pretty much all we have to do is traverse through the array and check the indexes which are the number of duplicates from the end of the array and compare them with the current index, and remove the duplicates. After that, just increment the duplicates integer by 1. Then, just return the duplicates integer. Here is what that looks like:

    And here are some examples of my output:

    Here are the logistics of my solution:

    I am more than satisfied with my results, and it was quite an interesting problem to complete. Anyways, if you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today and that you all have a great day!

  • LeetCode #12 – Integer to Roman

    The next problem that I will attempt to solve is:

    The approach I took for this problem is pretty much the same as the “Roman to Integer” problem, as they are very similar. Hashmaps are pretty much tailor-made for problems such as these, so once again, I will utilize one. Here is how it looks:

    My idea for this solution is to continually subtract the largest possible value available as a roman numeral away from the integer you are trying to convert, and then add the corresponding roman numeral to the output. Once you put all that together, you get this:

    While it was a very “brute force” way of approaching this problem, it clearly works:

    Here are the logistics of my solution:

    Anyways, that is all from me for now. I hope you guys at least learned something today, and that you all have a great day!

  • LeetCode #20 – Valid Parentheses

    Here is the next problem I will tackle:

    Here is some sample output, just to make sure you understand:

    Truthfully, I have actually done a problem like this before in my Computer Science class at school, however the way I did it was very inefficient, so it will be nice to solve it again, just much better. 

    Firstly, when I think of an efficient way to solve this problem, I think of using the Stack class, which is a data structure that utilizes a last in first out (LIFO) approach for processing data, by using the .push() and .pop() methods. Here is how you initialize it:

    As you can see, it is pretty much the same for most other data structures, such as ArrayLists, LinkedLists, Queues etc. My idea to solve this problem was to continually add the first parentheses ( ‘(‘ ‘[‘ ‘{‘ ), and then after that, pop any similar ending parentheses ( ‘)’ ‘]’ ‘}’ ). Here is how I implemented that:

    What this does, is it checks if c and cc are complementary parentheses, and if they aren’t then it returns false. Also, after finishing the whole for loop, you must check if the stack is empty or not, which will determine if there are any leftover parentheses, in which case it will return false. Here is how that looks:

    When you put all of that together, you get this:

    Here is the output:

    Here are the logistics for my solution:

    Anyways, that is all from me for now. If you have any comments or suggestions, make sure to let me know down below in the comments section. I hope you guys at least learned something today, and that you all have a great day!

  • LeetCode #5 – Longest Palindromic Substring

    This is the next problem that I will be solving:

    This problem, even though seemingly quite simple with its brief description, is more complicated than it looks. What I decided to do was do a roundabout approach that uses two “pointers”. The two pointers method iterates two pointers across an array, to track the start and end of an interval. Using this method, I was able to check the substrings that were palindromes from both sides, and see the lengths of them as well. Below is my final solution:

    When I ran this, my solution worked, and the runtime was surprisingly more efficient than I thought it would be, considering the amount of loops I wrote:

    That’s it for now, I hope you learned something, and I’ll have the next code uploaded soon!

  • LeetCode #7 – Reverse Integer

    The next question that I decided to tackle was:

    While it seems like quite a simple problem to just reverse the digits, it requires a bit of thinking on how to implement it. Here are some examples of input and output for this problem:

    So, it is a pretty self explanatory question and solution. What I did was I created a temporary long variable, output, which I would eventually return as the output for the method. I also created a while loop to take the end of the original number(input) and add it to the beginning of the output variable. Here is how I achieved that:

    After that, all we need to do is check to make sure that the output variable is within the constraints of a 32-bit integer. If it isn’t, then I would return 0. Here is what that looks like:

    If it is within the constraints, I cast the long output variable as an integer by doing this:

    Here is the whole solution:

    Here is my output:

    Here are the logistics of my solution:

    That’s the second 100% in a row! While not exactly ideal for memory usage, it still gets the job done. If you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today, and that you all have a great day!

  • LeetCode #10 – Regular Expression Matching

    I know it has been a while since my last post, and I apologize. I have been very busy in recent weeks, so I decided to take a little break. But, we’re starting again, and I should be able to get more posts out now. 

    Here is the description of the next problem:

    Here are some examples of input and output:

    As soon as I saw this problem, I thought that it was not hard at all to do, and you will see why soon. I was a bit confused why it said it was a ‘Hard’ problem. This is because I have actually seen a method in the Java API which deals with this problem. It is the ‘.matches()’ method. It will check whether or not the string matches the regular expression or not, and return true or false. That is exactly what this question is asking for. However, it will not be as efficient as manually coding this ‘.matches()’ method.

    Here is my implementation:

    Here is my output:

    Here are the logistics of my solution:

    While it is clearly not the fastest or most efficient solution, it is by far the cleanest and easiest to code. In the future, I will try to code it manually, just to get a more efficient solution. Anyways, if you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today and that you all have a great day!

  • LeetCode #35 – Search Insert Position

    Sorry for the delay between uploads. Here is the next problem I will tackle:

    Here are some examples of output:

    For this problem, you do not need to overthink the solution. It is quite easy. There are many possible ways to go about this problem. I decided to take the simple and clean route with my solution. Here was my plan:

    1. Traverse the array
    2. Check if the value of the array at each index was equal to the target
      1. If so, return the index value
    3. If not, then check if the value of the array at each index was less than the target
      1. If so, return the index value
    4. If you traverse through the whole array and still don’t return anything, then the target value is greater than the largest number in the array, and therefore, you can return the length of the array(which would return the index of a number that would be appended to the array)

    Here is how I went about doing that:

    While I know that a binary search would also be a very efficient way to solve this, memory usage wise, it was still very efficient runtime wise.

    Here are the logistics of my solution:

    Quite a good result, if I do say so myself. If you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today, and that you all have a great day!

  • LeetCode #1 – Two Sum (Again?!)

    Hello everyone, I know it has been a while, but here is my first attempt at coding using C++, which I have recently decided to take up. Its time to revisit the first ever problem solution which I have posted on this blog, except this time I use C++ instead of Java:

    This question, as it can be seen, is quite simple. Given the array nums, and integer target, the easiest way to solve this problem should be pretty apparent, and that is to just check every combination of indices until you have a sum that adds to target, and that is exactly what I did:

    Note: I made the int j = i +1, and not just 0, because j and i cannot be the same

    As you can see, C++ has a very similar syntax to that of Java, therefore it is a lot easier for me to pick it up due to my somewhat extensive knowledge of Java. 

    When I ran this method, it indeed gave the expected result:

    Results:

    Overall, not bad for my first time coding using C++. I am proud of what I have done so far, but I know that I have a long way to go. I plan on using more C++ in future blog entries, in order to help me grow my skills.

    That is all from me for now. I know it has been a while, but I will get back to posting more regularly. I hope you guys at least learned something today, and that you all have a great day!

  • LeetCode #14 – Longest Common Prefix

    Here is the next problem:

    Although the description doesn’t seem like much and the problem is labeled as ‘Easy’, it is in fact a bit challenging to code an efficient solution. Here are some examples of input and output:

    As you can see, the concept of the problem is quite simple, intuitively, however the execution of the solution is a bit more difficult to write in lines of code. Nevertheless, that is what I’m here for. Here is how I decided to write my solution:

    Let’s break it down. The first two lines(inside the brackets of the method), are a check to make sure there are no errors due to the array being empty:

    Next, I simply created a temporary local variable called prefix, and set it equal to the string in the first index of the array, strs:

    After that, I traversed through the array and made a nested while loop. The logic behind this was to check each other value in the array and compare how much of the first value in strs was the beginning of each other value in the array. Pretty much, the variable prefix is the entire first value, and every time that another value in the array does not start with prefix, the last character of prefix is taken off, and the process repeats until all the other values in the array begin with prefix. While that sounds like a lot, it is actually only 3 lines. After that, all you do is return prefix. Here is what that looks like:

    Once again, here is the final product:

    Here is my output:

    Here are the logistics of my solution:

    This is by far my best solution so far in all my solutions on my site, at least analytically speaking. Anyways, if you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today and that you all have a great day!

  • LeetCode #58 – Length of Last Word

    Here is the next problem:

    It is quite a simple problem, which has many different ways to solve it. Here are some examples of output:

    Also, here are the constraints for the problem:

    Even though this problem is labeled ‘Easy’, it becomes really easy if you know certain methods to manipulate the string. One example is the .split() method, which takes a string, and splits it into a String array, according to what parameter you put in. Here is an example of how it works:

    That is exactly how I used it for my solution. After that, I just returned the length of the string at the last index position, which is the length of the string array minus 1. That gives me the whole solution:

    A very efficient way of writing code, with only two lines, which is by far the least I had to write for any of these problems before. It also yielded some impressive results. Here are the logistics:

    While a slightly short solution, it was still a quite interesting problem, and I know for certain that there are many different ways of solving this problem. If you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today about the split method, or anything else, and that you all have a great day!