Posts

Showing posts from May, 2021

Java: Given a string, find the longest palindromic contiguous substring.

Image
Given a string, find the longest palindromic contiguous substring. If there are more than one with the maximum length, return any one.   For example, the longest palindromic substring of " aabcdcb " is " bcdcb ". The longest palindromic substring of "bananas" is " anana ". Approach Given String for example: “aabcdcb” -     Let’s split problem in to smaller parts. -     First lets chcek if the given String is valid or not if not, valid let’s return “ Invalid String ” response. -     Next, we can write a helper method that checks if given String is palindrome or not (I used StringBuffer reverse method to check the String) -     So, we have 2 utility methods lets concentrate on finding longest palindrome substring. (click on the image to expand) Note: ·          The above solution is not the ultimate or the only way to approach the problem. ·    ...

Java: Shift characters and compare

Image
  Given two strings A and B, return whether or not A can be shifted some number of times to get B.   For example,      if A is abcde and B is cdeab, return true.      If A is abc and B is acb, return false. The Problem is much similar to : Using String inbuilt methods Hope you can trace and understand easily 😊 Note: ·          The above solution is not the ultimate or the only way to approach the problem. ·          Attaching screenshot because you need to practice than copying code and executing in IDE. ·          As I said there are ‘N’ number of ways to solve the issue, here I tried using inbuilt methods from String class and get the solution. ·          Comments and suggestions are always welcome.  git link:   https://github.com/failed-peanut/...

Java: Reverse a singly linked list

Image
  Reverse a singly linked list     * Example:   Input : 1->2->3->4->NULL   Output: 4->3->2->1->NULL Approach: ·          Well, to approach this problem there are ‘N’ number of ways. ·          Use Stack data structure push Nodes, pop Node one by one and link to next popped Node. ·          Iterate through Nodes add it to List/Array iterate Array backwards. ·          Use recursive approach. ·          Use temp Nodes and change links iteratively. ·          Etc. Iterative Approach with using temp Nodes: ·          Assuming head is pointing to starting Node. Recursive Method: Note: ·          The above...

Java: Given a String find the distinct characters in it and count it

Image
  Given a String find the distinct characters in it and count it.   Input:   DISTINCT Output: DSNC 4 Approach: ·          Well, to approach this problem there are ‘N’ number of ways. ·          Convert string to character array. ·          Use HashMap to hold keys as characters and count as values. ·          Use any Set implemented Class add the characters and get the count. ( we can get distinct but need to remove repeated character ) ·          Use traditional Brute force method with multiple for loops, check each character and get the count. ·          Create your custom class with member variables as character and count add in to an ArrayList and get the count. ·        ...

Introduction to Failed Peanut!

Image
  … It all started with fails! Being failed is just stepping stones of success. I never thought I will write some thing like this, because I am very bad in writing 😊 I always learnt and learning in hard way. I am very tiered of getting rejected and failed. So thought of listing few of my experiences in to a blog that may help someone. I am learning and earning with Java. So technically this blog is on Java and even other programming languages too. Never ever think you can’t do it. I came from that background- a “dobutground” . Always had that fear and anxiety; can I, do it? Am I able to make it? Even the things which I know can I deliver it? One thing to note here is – “ Can I ?” – I always had doubt on me, possibly fear. There are hell extraordinary people in the world with great talents am I able to survive in this big talent pool? What if I may not able to succeed? Hell yea! I had so many such questions and doubts when I started. I have attended more than 500+ interviews...