LeetCode and Data Structures
Hi! đ Currently, Iâm preparing for my internship next year, and these days Iâm working hard on LeetCode problems.
At first, I thought it was enough to just:
- Try solving a problem
- Check the solutions
- Understand the idea
- Try it again
Well⌠it works. But honestly, youâll be faster (and way less frustrated) if you know some common patterns.
In the next few posts, Iâd like to write a series about the most popular techniques that can help you solve about 60â70% of problems on LeetCode (easy â medium level).
In this post, I want to introduce some basic data structures. Once you figure out how these work, youâll be able to understand most of the others much more easily.
Whatâs a data structure?
A data structure is just a way to efficiently store data. Thatâs it.
But itâs really important to choose the right one for the right task. Otherwise, youâll waste time (and get frustrated) trying to figure out which operations make sense for the structure you picked â or why your time complexity suddenly jumps to O(n^2) just because you tried to add a few characters to a string.
1. Array
String[] myFamily = {"Rick", "Morty", "Summer", "Beth", "Jerry"};
Array is a linear, continuous data structure used for storing data.
Most common example: think about a row of lockers with numbers (indices). You open one and put your data inside.
Some features:
- Strings are arrays of characters.
- Access time:
O(1)â you know your locker, just open it and get your value. mmm, nice, love it. - Insert/Delete at the end/start:
O(1) - Insert/Delete in the middle:
O(n)(notn^2, so not terrible â just shift N elements).
Where to use:
- Traversing a structure in order
- Accessing specific indices
- Comparing elements from both ends
- Sliding window, prefix sum, etc.
2. String
String heroName = "Morty";
Well, strictly speaking, string is not a data structure, but it deserves to be mentioned.
A string is basically an array of characters.
Main things to remember:
- In memory, a string really is an array of characters.
-
Immutable in most languages: for example, when you do
"dog" + "s", it doesnât change the first string â it creates a totally new one.
When to use it:
- Checking for anagrams
- Returning a substring that matches a pattern
- Finding the longest substring without repeating characters
- String problems are not for brute-force solutions â youâll need smart techniques.
3. Set
HashSet<Integer> mySet = new HashSet<>();
mySet.add(0);
mySet.add(1);
mySet.add(6);
mySet.add(5);
Set is a collection of unique variables. Thatâs it.
Some features:
- Access time:
O(1)(usually) - Stores only unique elements
Where to use it:
- When you need uniqueness
- When you want to check for existence
- When you need fast membership checks (
ArrayneedsO(n),Setdoes it inO(1))
4. Map (Dictionary)
HashMap<String, String> contacts = new HashMap<>();
contacts.put("me", "123-123-123");
contacts.put("dad", "000-000-000");
A map is a collection of keyâvalue pairs. Like a phone book: name â phone number.
Features:
- Access:
O(1)(usually); worst case isO(n)but rare - Keys must be unique
Where to use it:
- Counting frequencies
- Caching
- Mapping indices
Summary
Thatâs it for the basic data structures.
All the others (trees, graphs, linked lists, etc.) are like upgraded versions of these four. If you nail these, youâll pick up the rest much more easily.
In my next posts, Iâll cover common patterns and real LeetCode problems.
I hope you liked this post â see you later! đ
Top comments (2)
This feels like the âstarter packâ every beginner wishes they had. Arrays are basically lockers, strings are lockers with letters, sets are lockers where the guard wonât let duplicates in, and maps are just that one friend who remembers everyoneâs phone number. Looking forward to the sequel of this sitcom.
Thanks for your comment! I agree with you â it really is the basics starter pack.
Personally, in the beginning I thought there were so many data structures that it would take forever to learn them all. But in the end, you just need a solid grasp of the basics â and youâll be good đ