Longest Palindromic Substring

Today, we will discover one of the basics algorithms to find the longest palindromic substring.

Problem:
Given a string S, find the longest palindromic substring in S.

Difficulty: Easy

Practice: Leetcode

Examples:

1. S = "abad", Result = "aba"
2. S = "codeedoc", Result = "codeedoc"

Solution:
To come up with a solution we need to understand what the palindrome is. Basically, this is just a word which reads the same backward as forward (for more details see).

The main idea is to find all palindromes in S with even and odd lengths and keep track which one is the longest. To find odd length palindromes we fix center for every letter in S and expand it in both directions.

Longest Palindromic Substring

To find even length palindrome we fix center between two nearby letters in S and also expand it in both directions.

Longest Palindromic Substring

While expanding, if two opposite letters are equal then we add them to our potential palindrome.

Time complexity: O(N^2)
Space complexity: O(1)

That’s all for today. You can find the source code here, but I suggest you implement it by yourself.

Thank you for reading my blog and see you in a leaderboard :)

Advertisements

Longest Common Substring

In this post, we will discover the basic algorithm to find the longest common substring of two strings.

Problem: Given two strings X and Y, find the longest common substring.

Difficulty: Easy

Examples:

1. X = "timetocode", Y = "timerightnow", Result = "time"
2. X = "abcabdef", Y = "abdf", Result = "abd"

Solution: Dynamic Programming
To solve this problem, we need to find all occurrences of all X letters in Y string. To keep intermediate results we can use a two-dimensional array of length X and Y accordingly.

Longest Common Substring

Even now we can see the result. But to make it more clear, let’s add some rules how to fill this table:
1. If X letter is not equal to Y letter then Table[i][j] = 0.
2. If X letter is equal to Y letter then this is already common substring of length 1. If the previous letter was also substring then the length of a new substring is Table[i][j] = Table[i - 1][j - 1] + 1 (prev length + current length).

Longest Common Substring

Obviously, the length of the longest common substring is the maximum value in this table. And the actual substring can be reproduced by previous cells from the cell with maximum value to the cell with a value of one.

Time complexity: O(N^2)
Space complexity: O(|X| * |Y|)

The source code for this problem you may find here. Thank you for reading my blog and have a nice coding!

Level up VIM skills

Level up VIM skills

Over two years I’ve been using VIM as the main development tool. From plugin for Visual Studio to complete IDE for JS and Go projects. In this post, I’m going to level up your VIM experience by sharing my best practices, most used hotkeys and awesome plugins. If you are interested in then welcome into my VIM-world. But if don’t know what the hell it is or you don’t have enough confidence when using VIM, I recommend you to read the set of articles about VIM for beginners (one, two, three).

Continue reading “Level up VIM skills”

Weird JavaScript: How Equals Operator Works

Hi, friends. As you know JavaScript is a kind of weird language, at least it seems like for the first time. And what I am going to do is to improve your impression with this category of articles. So welcome to the Weird JavaScript.

And in this first post we are going to talk about equals operator (==). Is there anything can be unclear here? Definitely, everything is understandable for you, just make sure you can answer this questions without any hesitation.

1 == '1'
[] == false
false == {}
NaN == NaN
+0 == -0
null == null
null == undefined
[] == []

If you are not sure in your answers or you just want to get the results, this short post is worth your attention.

Continue reading “Weird JavaScript: How Equals Operator Works”

Das Keyboard Ultimate 4: Brave Decision

Das Keyboard Ultimate 4: Brave Decision

Mechanical keyboards offer a kind of another level of typing experience. These are keyboards which facilitate interaction by providing you a tactile feedback. They are accurate, responsive, extremely durable as long as expensive. Today we are going to speak about a good candidate to be the best mechanical keyboard. Das Keyboard Ultimate 4 which is completely blank with Cherry MX for bad ass coders and gaming enthusiasts.

Continue reading “Das Keyboard Ultimate 4: Brave Decision”

Graphs and trees visualization with DGML

Graphs and trees visualization with DGML

Recently, I had a task to display dependencies between project files according to some rules. I built a tree structure with all necessary information, but the most interesting part was to display it. In this post I will tell you about the easiest solution and maybe the best one I’ve found. If you are interested in, then welcome under the cut.

Continue reading “Graphs and trees visualization with DGML”