<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anuj Kumar Jha</title>
    <description>The latest articles on DEV Community by Anuj Kumar Jha (@anuj).</description>
    <link>https://dev.to/anuj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F135745%2Fc6e972b7-1f94-4cbd-bbc1-758a9f24a9c6.jpg</url>
      <title>DEV Community: Anuj Kumar Jha</title>
      <link>https://dev.to/anuj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anuj"/>
    <language>en</language>
    <item>
      <title>Algorithm: Longest Continuous Palindrome in a String</title>
      <dc:creator>Anuj Kumar Jha</dc:creator>
      <pubDate>Sat, 20 Apr 2019 16:21:38 +0000</pubDate>
      <link>https://dev.to/anuj/algorithm-longest-continuous-palindrome-in-a-string-373d</link>
      <guid>https://dev.to/anuj/algorithm-longest-continuous-palindrome-in-a-string-373d</guid>
      <description>&lt;p&gt;Prerequisite: Basic programming experience in any language&lt;br&gt;
Problem: Given a string find the longest continuous palindrome in it. We should not change position of any character, instead we should find how many palindromes are there and which is longest among them. For ex: if given input string is "madam" then possible palindromes are ("m", "a", "d", "a", "m", "ada", "madam") output would be "madam" as its longest palindrome. Or if given input is "Crore" output would be "ror"&lt;br&gt;
If you still have confusion in understanding the problem, refer below link&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/longest-palindrome-substring-set-1/"&gt;https://www.geeksforgeeks.org/longest-palindrome-substring-set-1/&lt;/a&gt;&lt;br&gt;
For this problem we can assume given input contains all lower case character only.&lt;br&gt;
I will write code in Java, But you can apply this logic to any language of your choice.&lt;br&gt;
Approach 1- Brute Force: &lt;br&gt;
 A brute force solution would be to consider/generate all substring and check if they are palindrome and find the largest among the string which are palindrome. This algorithm will run in O(n³) considering n is length of the string.&lt;br&gt;
Approach 2:&lt;br&gt;
 We can reduce the time complexity to O(n²) by following below method.&lt;br&gt;
To solve this problem we can try to find largest palindrome from each index. For each index we will keep two pointer start and end. start index will represent left side and end index will be right side. We will check if character at both index is same then we will move start to further left and end to further right. We keep doing it until the character at left and right index are not same. Finally our substring would be palindrome. This result we will compare with the length of previous result and if its greater we will update the result. Below is the complete code for this&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static String longestPalindrome(String str) {
    if (str == null || str.length() &amp;lt;= 1) return str;
    String res = str.substring(0,1);
    for (int i = 0; i &amp;lt; str.length(); i++) {
        String str1 = expands(str, i, i);

        String str2 = expands(str, i, i + 1);
        if(str1.length()&amp;gt;res.length()){
            res = str1;
        }
        if(str2.length()&amp;gt;res.length()){
            res = str2;
        }

    }

    return res;
}


private static String expands(String str, int start, int end) {
    while(start&amp;gt;=0&amp;amp;&amp;amp; end&amp;lt;str.length() &amp;amp;&amp;amp;  str.charAt(start)==str.charAt(end)){
        start--;
        end++;
    }
    return  str.substring(start+1, end);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This algorithm run has time complexity of O(n²).&lt;br&gt;
Approach 3:&lt;br&gt;
 If you see previous problem we are creating many substring which is memory wastage.We can still improve our algorithm to avoid creating substring each time and instead return length from expand method. And based on length we can have our start and end index which can be used to extract final subString. Below is the code to do this.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static String longestPalindromic(String str) {
    if (str == null || str.length() &amp;lt;= 1) return str;
    int start = 0;
    int end = 0;
    for (int i = 0; i &amp;lt; str.length(); i++) {
        int len1 = expand(str, i, i);
        int len2 = expand(str, i, i + 1);
        int len = Math.max(len1, len2);
        if (len &amp;gt; end - start) {
            start = i - (len - 1) / 2;
            end = i + len / 2;
        }
    }
    return str.substring(start, end + 1);
}


private static int expand(String str, int start, int end) {
    while (start &amp;gt;= 0 &amp;amp;&amp;amp; end &amp;lt; str.length() &amp;amp;&amp;amp; str.charAt(start) ==     str.charAt(end)) {
        start--;
        end++;
    }
    return end - start - 1;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Thank you for reading. Do comment if any bug found.&lt;br&gt;
You can follow me on Twitter and GitHub&lt;/p&gt;

</description>
      <category>programming</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Understanding Handler, Looper and Handler Thread</title>
      <dc:creator>Anuj Kumar Jha</dc:creator>
      <pubDate>Sat, 16 Feb 2019 06:14:52 +0000</pubDate>
      <link>https://dev.to/anuj/understanding-handler-looper-and-handler-thread-3anf</link>
      <guid>https://dev.to/anuj/understanding-handler-looper-and-handler-thread-3anf</guid>
      <description>&lt;p&gt;In my first ever article I am going to try to simplify the Handler, Looper and Handler thread class and the relation between them. Till now I have been struggling to understand the relation between them. So I thought of simplifying this for other people like me.&lt;/p&gt;

&lt;p&gt;Let’s get started.&lt;/p&gt;

&lt;p&gt;First of all lets see what is Handler Thread: Handler thread is nothing but a simple thread(as in Java) which execute certain task. It eventually extends the Thread class and also implement the run method of it. And to start it we can do Thread.start() method as with all others threads in Java.&lt;/p&gt;

&lt;p&gt;So what is the difference?&lt;/p&gt;

&lt;p&gt;So Imagine that you run into a problem where you want your thread to be running and you being able to post/send task to it so that it executes them basically what I mean is you want to reuse your thread. The way to do this in Java is&lt;/p&gt;

&lt;p&gt;Keep a thread alive i.e. do not let it come out of run method.&lt;br&gt;
Maintain a queue and post your task in it.&lt;br&gt;
Process the queue in your run method ie execute task one by one or when it comes&lt;br&gt;
Finish/Terminate the thread once done&lt;br&gt;
I guess you see the issue that you have to manage lots of things and if not lot then at-least you should be very careful with this approach. But the good thing about Handler thread is it you can avoid all these. It comes with a message queue associated which can be used to send any message/task to this thread once it’s started. So how does Handler Thread does it?&lt;/p&gt;

&lt;p&gt;Android has 3 main components to handle these which is used by HandlerThread. Let’s go through them once.&lt;br&gt;
 &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31z81y7xh2bhqpgwn04q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31z81y7xh2bhqpgwn04q.png" alt="Handler" width="632" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looper: Looper is a worker that keep a thread alive, It loops over message queue and send the message to respective Handler.&lt;/p&gt;

&lt;p&gt;Handler: This class is responsible for enqueuing any task to message queue and processing them. Each Handler can be associated with one single thread and that thread’s message queue.&lt;/p&gt;

&lt;p&gt;Message Queue: This class holds the list of messages to be dispatched by the looper. You can just call Looper.myqueue() to get list of messages. We do not normally deal with it.&lt;/p&gt;

&lt;p&gt;Of-course you can use above three components with normal thread class also. You can create your own thread and use all above mentioned components. The process to do this is mentioned below&lt;/p&gt;

&lt;p&gt;Create a Thread class&lt;br&gt;
Call Looper.prepare inside run method.&lt;br&gt;
instantiate your Handler class and implement handleMessage method.&lt;br&gt;
Call Looper.loop()&lt;br&gt;
I can write the code to do this but since I do not want you to follow that approach I will skip that. As this is not very efficient way, so Android has made job simpler by introducing HandlerThread.&lt;/p&gt;

&lt;p&gt;We can write our own class which extends HandlerThread.. Each HandlerThread class has a looper associated with. We can use this looper to create Handler and then we can start enqueuing/sending message to this thread.&lt;/p&gt;

&lt;p&gt;Steps to do it.&lt;/p&gt;

&lt;p&gt;Create a class that extend HandlerThread&lt;br&gt;
Instantiate your handlerThread class&lt;br&gt;
call start method // your thread is running now&lt;br&gt;
Create/Instantiate your Handler by using looper from handler thread created above&lt;/p&gt;

&lt;p&gt;public abstract class BaseHandlerThread extends HandlerThread&lt;br&gt;
{&lt;br&gt;
public BaseHandlerThread(String name)&lt;br&gt;
{&lt;br&gt;
    super(name);&lt;br&gt;
}&lt;br&gt;
&lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt;&lt;br&gt;
protected void onLooperPrepared()&lt;br&gt;
{&lt;br&gt;
    initHandler();&lt;/p&gt;

&lt;p&gt;}&lt;br&gt;
private void initHandler()&lt;br&gt;
{&lt;br&gt;
    mHandler = new Handler(getLooper()) {&lt;br&gt;
        &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt;&lt;br&gt;
        public void handleMessage(Message msg)&lt;br&gt;
        {&lt;br&gt;
           // &lt;br&gt;
        }&lt;br&gt;
    };&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
We can simply create instance of BaseHandlerThread and then call start() method on it. Once start is called Looper gets prepared. After Looper is prepared we get a callback where we are initialising the Handler. A handler can only be initialised after looper os prepared. Now you can use this handler to enqueue message. &lt;br&gt;
When you want to terminate this thread just call quitSafely() method on your handler thread instance or quit() for OS version older then JELLY_BEAN_MR2.&lt;/p&gt;

&lt;p&gt;When you do not want to create separate class and still want to use HandlerThread you can follow below step. Note that When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it. So be very sure about how you are creating the Handler.&lt;/p&gt;

&lt;p&gt;//Create Handler&lt;br&gt;
mHandlerThread = new HandlerThread("name");&lt;br&gt;
mHandlerThread.start();&lt;br&gt;
mHandler = new Handler(mHandlerThread.getLooper());&lt;br&gt;
Thats about it. Feel free to share your input and point to improve on.&lt;/p&gt;

</description>
      <category>android</category>
      <category>thread</category>
      <category>handler</category>
      <category>handlerthread</category>
    </item>
  </channel>
</rss>
