Day 51

Hey guys, I hope your Tuesday was amazing! For today’s algorithm problem, I’m going to write a function that can take in an array of length 2n, and returns a new array with the inputs shuffled. Here’s a bit more insight into what I mean:

Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].Return the array in the form [x1,y1,x2,y2,...,xn,yn].

And here’s an example:

Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7]
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].

To start off, I created a new array and began…


Day 50

Hey everyone, I hope your week was off to a great start! For today’s algorithm problem, I’m going to write a function that can see how many steps have been taken to edit a string, and if only one has been taken, the function would return true. The steps you can perform on a string are either:

  • Changing one character
  • Deleting one character
  • Inserting one character

Here’s an example:

Input: pale, bale
Output: True
Input: bake, pale
Output: False

To start I thought about how I could compare the two strings, and what their lengths would mean. For example, if…


Day 49

Hey everyone, I hope you had an amazing weekend! Did you guys make some time for yourselves or did you do some extra work to help you later in the week?? Let me know! For today’s algorithm problem, I’m going to write a function that can return true or false if an inputted string can be permutated in any way to create a palindrome. Here’s an example:

Input: Tact Coa
Output: True (permutations: "taco cat", "atco cta", etc.)

In order to solve this problem, we have to actually think about what a palindrome looks like. Well, for starters it should…


Day 48

Hey everyone, I hope your Saturday was amazing and your weekend was off to a great start! For today’s algorithm problem, I’m going to write a function that can turn a string with spaces into a URL. Here’s an example:

Input: "Mr John Smith"
Output: "Mr%20John%20Smith"

Do you guys remember how often I use JavaScript’s built-in functions? A lot! So why not use them one more time right? For this one, I’m going to use .split() and .join(). The first one will be used to split up each word with a space before or after it, while the second one…


Day 47

Hey guys, I hope your Friday was amazing and you had a productive work week. For today’s algorithm problem I’m going to write a function that` can return true if all characters in a string are unique and false if there are repeating characters. Here’s an example:

Input:"123456^&*(-=";
Output: True
Input:"1124356789*&^%";
Output: False

So I started out by doing some research and found out that there’s basically only 128 possible unique string characters that you could use. Here’s a quick link to help you review:

Knowing this made me write my first logic statement where if the input’s length was…


Day 46

Hey guys, I hope your Thursday was amazing! For today’s algorithm problem, I’m going to design a function that can tell you how many ones are appearing consecutively in a binary array. Here’s an example:

Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.

I thought this answer was pretty simple, and I started out of course, by declaring a max variable and a count variable. I then iterated through the array and if there are consecutive ones, the count gets incremented each time…


Day 45

Hey guys, I hope your Wednesday was great! For today’s algorithm problem, I’m going to design a function that takes in an array of integers and returns a separate array which tells you how many numbers are lower than each entry of the original array. Here’s an example:

Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1). …

Day 44

Hey guys, I hope your Tuesday was amazing! For today’s algorithm problem, I’m going to create a function that can detect what the common characters are in an array of strings. Here’s an example:

Input: ["bella","label","roller"]
Output: ["e","l","l"]
Input: ["cool","lock","cook"]
Output: ["c","o"]

I started off by iterating through the array and making a copy of the original array’s first element. The consequent iterations would compare each entry to the other entries by using a .filter() built-in method. I filter the new array and return the array once it goes past a certain length, the length of the original array. …


Day 43

Hey guys, I hope you all had a great day and our week was off to a great start! For today’s algorithm problem, I’m going to write a function that takes in an array and a value and returns the same array with the value taken out of the array. Here are a couple of examples:

Input: nums = [3,2,2,3], val = 3
Output: [2,2]
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: [0,1,3,0,4]

So when I started out solving this problem, I thought of having two pointers run through the array and having a counter incremented each time an…


Day 42

Another Sunday, and another week has gone by! I hope all of you guys were productive this weekend and got stuff done. For today’s algorithm problem, I’m going to write a function which basically takes in an array and returns true if there are any duplicates, and false if there aren’t any. here’s an example:

Input: [1,2,3,1]
Output: true
Input: [1,2,3,4]
Output: false
Input: [1,1,1,3,3,4,3,2,4,2]
Output: true

So when I started solving this problem, I sorted the input array and just decided that if a number showed up more than once, the function would return true once I start iterating…

Setu Potnis

A university student wanting to learn more as well as share my journey about learning software

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store