Here is one other Leetcode problem where my solution is accepted.
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.
Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
Input: moves = "UD" Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
Leetcode problem: Implement strStr()
Here's another Leetcode problem where my solution is accepted. Sharing it here because this is a very basic example of usage of substring() and equals() method in String class.
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Clarification: What should we return when needle is an empty string?
For the purpose of this problem, we will return 0 when needle is an empty string.
I have started playing with LeetCode. Here is a problem I found, and here is my accepted solution.
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
Input: nums = [1,3,5,6], target = 5 Output: 2
Playing with HashMap
HashMap, as I mentioned before, is a key-value store. You place an object into this collection by specifying a "key".
You can then retrieve the object by using the key.
Let me show you with code examples:
In the example above, I am creating a map of Country-> Capital city. You can see the usage of put(), size() and get() methods to demonstrate how you can use the key-value store
Playing with ArrayList
ArrayList is very useful in cases when the list needs to be modified a lot. It also offers us "positional access" - meaning if you want to know what object is at a specific index, you can directly access it.
The methods are named very simple and intuitive:
I think looking at the code, it's very self-explanatory:
In the code above, I have shown how to use add(), get(), remove() methods in ArrayList. As you can see, it's really easy!
A collection basically is a "group of objects". Java provides excellent support for various types of Collections.
At a high level, the types of Collections are List, Set and Map.
We'll go into each of these Collections. Here is a broad view of the hierarchy:
Putting Inheritance and Polymorphism together
I had given examples of Inheritance and Polymorphism previously, and I also talked about method overriding.
I found this video super-helpful to put all these concepts together. It's less than 10 mins, please don't miss it!
This is a very important concept in Object Oriented Programming.
Polymorphism basically means taking multiple forms. (Poly = many, Morph = form)
For example, take me. In my school, I am a Student. For my brother, I am a Teacher. I am also a Friend, a Son, a Grandson.
A person at the same time can have different characteristics -- this is called polymorphism.
Here is a super-woman (like my mom) doing multiple things. She is an example of Polymorphism.
Java supports both compile time polymorphism and runtime polymorphism.
I will explain this using a code example.
When we run this, the output is:
I am Child1
I am Child2
What happened here is method overriding. The child classes overrided the functionality of hello() implemented in the super class.
This is a real basic but important example of Polymorphism. It's that simple! When I first heard about "Polymorphism", I was baffled, but hopefully this simple example helps you understand this concept!
In the previous blog, I talked about Inheritance using the example of Animals (Dog and Cat).
I found a great video explaining inheritance using another example. It's only 10 mins long, and is really worth it!
Inheritance in Java
Inheritance is an important concept in Object Oriented Programming, also called OOPS. It allows one class to inherit the attributes and methods of another class.
Some important terms:
Cats are animals. Dogs are also animals.
They both eat.
They both have 4 legs.
They both make sound. While a cat says "meow", a dog says "woof".
So why not take the common things between them, and put it in an Animal class? We can then have both Dog class and Cat class "inherit" stuff from Animal?
Let me show with some code:
Now, as you can see, both Dog and Cat "inherited" the attribute: numberOfLegs and the eat() method.
They are able to "override" the makeSound() method with their own implementation.
This is what is called Inheritance. Seriously, it's that simple!
Here is a simple diagram illustrating this concept.