Showing posts sorted by relevance for query 214 shortest palindrome. Sort by date Show all posts
Showing posts sorted by relevance for query 214 shortest palindrome. Sort by date Show all posts

Saturday, August 17, 2019

214. Shortest Palindrome

August 17, 2019

Introduction


It is the good idea to show how to learn to solve a hard level algorithm. I believe that the learning process will help me to overcome difficulty in my daily job and help me to prepare more challenging programming task. I tried so many times to learn to solve 214 Shortest palindrome recently, more than three times I asked the algorithm in the mock interview as an interviewer.

Case study


I did write a post to share my understanding today. Here is the link.

It is so challenge to learn KMP algorithm and understand how to construct KMP table. I learned quickly by watching the video.
From 5:37 - 8:04, the explanation how to build longest prefix and suffix table is easy for me to follow,
a b c d a b c a
0 1 2 3 4 5 6 7
0 0 0 0 1 2 3 1 <- Lps Array (longest prefix suffix array)
I will add more explanation how to solve the problem later.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _214_shortest_palindrome
{
    class Program
    {
        static void Main(string[] args)
        {
            RunKMPTableTestcase();
            var result = ShortestPalindrome("aacecaaa");
        }

        /// <summary>
        /// study video 
        /// https://www.youtube.com/watch?v=GTJr8OvyEVQ
        /// Knuth–Morris–Pratt(KMP) Pattern Matching(Substring search)
        /// 5:37 - 8:04
        /// </summary>
        public static void RunKMPTableTestcase()
        {
            // KMP table should be [0,0,0,0,1,2,3,1]
            var lps = ComputeLpsArray("abcdabca");
        }

        public static string ShortestPalindrome(string s)
        {
            var charArray = s.ToCharArray();
            Array.Reverse(charArray);
            int[] table = ComputeLpsArray(s + "." + new string(charArray));
            charArray = s.Substring(table[table.Length - 1]).ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray) + s;
        }

        /// <summary>
        /// study code 
        /// https://leetcode.com/problems/shortest-palindrome/discuss/350795/C-Solution-O(n)-using-%22longest-prefix-suffix-array%22-of-KMP
        /// longest prefix and suffix array
        /// Detail explanation - 
        /// study video 
        /// https://www.youtube.com/watch?v=GTJr8OvyEVQ
        /// Knuth–Morris–Pratt(KMP) Pattern Matching(Substring search)
        /// 5:37 - 8:04
        /// a b c d a b c a
        /// 0 1 2 3 4 5 6 7
        /// 0 0 0 0 1 2 3 1
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static int[] ComputeLpsArray(string str)
        {
            var length = str.Length;

            // default is 0 
            var lps = new int[length];

            for (int i = 1; i < length; i++)
            {
                int j = lps[i - 1];
                 
                while ((j > 0) && (str[i] != str[j]))
                {
                    j = lps[j - 1];
                }

                lps[i] = str[i] == str[j] ? j + 1 : j;
            }

            return lps;
        }
    }
}


Sunday, August 4, 2019

Case study: two hours mock interview with a friend

August 4, 2019

Introduction


I had a two hour mock interview with a friend met on interviewing.io. I like to write down some notes and help me track how many things I should work on next two weeks.

Case study


I am preparing Amazon and Facebook onsite interviews in August. He is preparing Google and Amazon onsite interviews in August.

We met on pramp.com. I gave him the algorithm to work on for warmup. The algorithm is called is graph bipartite. Here is his code with my review, I added all comments.

He told me that he always using graph algorithm pseudo code as a template.

He likes to give me an algorithm to work on after we spent 30 minutes. I chose to give him a hard level algorithm to work on since I like to learn KMP algorithm from him.

We had discussion about KMP algorithm, he showed me his code to use longest prefix suffix array to solve the problem.

Next he gave me the algorithm to work on. Find if there are increasing subsequence with length three in the array. I did not come out the correct answer, I came out partial solution to see if maximum length of any continuous increasing subarray is bigger than two.

He showed me that it is special case of longest increasing subsequence. He then showed me O(nlogn) solution using binary search.

He also showed me his solution for hard level algorithm Leetcode 214: shortest palindrome. He told me that he submitted the code while I coded my solution.

Actionable Items


The mock interview partner learns things quickly. He showed me two solutions, one is binary search to solve longest increasing subsequence, and then second one is hard level 214 shortest palindrome using same function used for KMP algorithm - longest prefix suffix array.

What I did is to show him how I can write a simple while loop algorithm and test the code using pramp.com, and then he showed me a failed test case. I explained the idea to use extra space to help and using linear time to solve the algorithm, he explained to me to work on the idea to use binary search.






Tuesday, February 6, 2018

Leetcode 214: Shortest palindrome (I)

Feb. 6, 2018


Introduction


It is hard level algorithm and I like to learn the algorithm. It is called Shortest palindrome.

30 minutes thinking


Since the algorithm is hard level, I plan to practice at least 10 times. Today I spent 30 minutes to thin k about the solution, and address possible issues in the brute force solution.

Follow up

Feb.15, 2018

I did spend over 30 minutes to read the discussion, and then wrote C# solution based on Java code. I did not fully understand KMP algorithm yet.


Sunday, December 6, 2020

Algorithms to review: Dec 6 2020 Last six months

 分类整理

https://www.1point3acres.com/bbs/thread-649468-1-1.htm
分贴(一)分类整理追加单调队列
https://www.1point3acres.com/bbs/thread-651314-1-1.html
分贴(二)分类整理之Stack栈
https://www.1point3acres.com/bbs/thread-683612-1-1.html

6个月内
Easy 1592, 1170, 415, 387, 345, 157, 38,  20,14
Medium 791, 22,647,609,49,249,833,159,1138,767,17,227,93,722,43,809,3,5,165,
Hard 527, 72, 340, 76, 158, 214, 68, 10, 44

6个月前
Easy  800,293,521,1422, 1544,520,758,551, 925,
Medium  544, 890, 1525,1016,1062,1618,1023,1452,583,816,809,1461,1513,385,1616,522,686,271,
Hard 632, 736, 1316, 1585, 1449, 1392, 591

Hard level
214. Shortest Palindrome
KMP algorithm - Explanation in Chinese, here is the link. 
68 Text Justification
632. Smallest Range Covering Elements from K Lists

Study grandyang leetcode blog. 

Monday, September 2, 2019

10 minute exercise - find longest common prefix and suffix in the string

Sept. 2, 2019

Introduction


It is a drill for hard level algorithm 214 shortest palindrome. What I like to do is to write  a blog to calculate longest common prefix and suffix for a string.

My drill 


a b c d a b c a
0 1 2 3 4 5 6 7
0 0 0 0 1 2 3 1 <- Lps Array (longest prefix suffix array)

For example, string a b c d a b c 
prefix: abc
suffix: abc

"abc" is longest common prefix and suffix, so the table should be updated

a b c d a b c 
0 1 2 3 4 5 6 <- index 
0 0 0 0 1 2 3 <- longest prefix and suffix 


Actionable Items


I think that it is important for me to practice more often the simple drills and therefore I can prepare to work on hard level algorithm. 

Thursday, August 1, 2019

214. Shortest Palindrome

August 2, 2019

I have fun to read the article, I spent more than 30 minutes to read. I like the writing as well. This should be the first time I read the article!

10 minutes reading:

Read KMP algorithm through KMP algorithm

Here is the wiki article.


Spend 30 minutes to learn how to build KMP prefix table

Read the discussion post here.

Here is C# code I like to study.

Sunday, August 4, 2019

Case study: KMP algorithm to study

August 4, 2019

I had two hours mock interview with the engineer who prepares Google onsite from 10:00 PM to 12:00 PM today. I asked him to solve hard level algorithm 214. Shortest Palindrome. We had discussion about the algorithm can be solved using KMP algorithm. He shared with me his C# code for KMP algorithm. 


Here is C# code. 




Wednesday, August 5, 2015

Leetcode questions and web link

August 5, 2015

  Here is the table about leetcode questions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
179
186
187
188
189
190
191
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251