Introduction of Dsa
Introduction of Dsa
CP
vs DSA?
CP vs DSA
How is CP and DSA different?
Technically, CP is a sport version of
DSA.
However, we often use “DSA” to refer to
the action of people learning DSA for
interview preparation or college exams
whereas the term “CP” is used to refer
to the action of people participating in
the coding competitions / sport.
Choice of
Language
All of us wants to stay in our comfort zone.
But it’s very difficult to write code in every
language or more than 1 language in a live
class.
We will try keeping most of our classes
language agnostic. We will focus more on
how to think about the problem and solve it
than writing the code. However, in some
cases I will be writing some code - we can
decide on C++ / JS or do alternatively.
However, you will also get the code in all
other languages in the reading materials.
My belief
Operations
Creating an array / list
For strongly typed languages you
need to provide a type
C++ code
JS code
const arr = []
const arr = [1, 2, 3, 4]
const arr = new Array(5) // Not fixed size
Strings
Array / List of characters is called a string.
It is a collection of alphabets, words or
other characters
Example:
"Hello world"
"Hello"
"Hello, I am 2020 graduate"
Problem 1: Good
Pairs
Problem Link:
https://leetcode.com/problems/number-of-
good-pairs/description/
Example 1:
Example 2:
Example 3:
Solution
Initial approach
Let’s consider a different problem. Given
a part of the array [0…i] where i can be
anything, can we find how many
elements are there that are equal to
arr[i] in this part of the array?
We can run another loop j from 0 to i-1
and check if arr[j] = arr[i] and
increase count.
Now do the above logic for all i from 1 to
n-1.
Optimal solution
Instead of running 2 loops, let’s see if
we can do with one loop. We are running
the loop of i. What do we need? No. of
elements on the left that are equal to
arr[i] right? And what are the elements
on the left? It is same as the elements
that we have visited in the loop till now,
isn’t it ?
So if we can maintain a frequency for all
the elements that we have visited till
now, in an array called freq , then
freq[arr[i]] can be added to the ans.
Problem Link:
https://leetcode.com/problems/two-
sum/description/
Solution
The problem reduces to the above
problem, instead of finding frequency of
arr[i] we just need to find target -
arr[i] .
Problem 2: Chef
Problem 2: Chef
and Happy
Strings
Problem Link:
https://www.codechef.com/practice/course/
strings/STRINGS/problems/HAPPYSTR
Chef has a string S with him. Chef is happy
if the string contains a contiguous
substring of length strictly greater than 2
in which all its characters are vowels.
Determine whether Chef is happy or not.
Example 1:
Input: aeiou
Output: "Happy"
Explanation: "aei" is one contiguous substring with leng
Example 2:
Input: abxy
Output: "Sad"
Explanation: There is no substring of length > 2 contain
Solution
Brute force
Generate all substrings with length > 2
and check if anyone of them is
completely made of only vowels.
bool checkOnlyVowels(string s) {
// Returns true if only vowels in s, false otherwise
}
return false;
Optimisation
Observation: If a substring of length 4 or
more has only vowels, then any
substring of that substring of length 3
will also have only vowels. Thus, we can
only focus on substrings of length 3.
Problem 3
Example 1:
Input: s = "ababcba"
Output: "ba"
Explanation:
Solution
Brute force
We can do the moves iteratively, till we
reach empty string. Also, before we do a
move we need to maintain the last string
value as well. Once we reach empty
string, the ans is the last string value.
// Pseudo code
function make_move(s) {
// remove first occurence of each character of s an
}
last_val = ""
while (s is not empty) {
last_val = s
s = make_move(s)
}
Optimisation