Friday, 1 May 2020

longest common prefix (leetcode) java implementation

Hi Lets solve the Leet code problem "Longest Common Prefix"

Problem :

        we were given an array of string  we should return the Longest common prefix

 Input: ["flower","flow","flight"]
Output: "fl
here the largest matching prefix is fl
Code:
Test Cases:

if you have any query or suggestion please feel free to post a comment

Thursday, 30 April 2020

palindrome Number (leet code) implementation in java

Hi  in this blog post we are going to implement the leet code problem palindrome Number 

Problem Statement :

       given a number we have to determine weather it is palindrome or not  , (i.e) it reads the same if reads backwards as well as forwards 

for eg 121 is palindrome 


how do we solve the problem , there are lot of ways to solve the problem the approach i used was Two pointer approach , using two pointer one at the beginning and another at the end if they didn't match then it is not a palindrome if it matches then we increment the starting pointer and  decrement the endpointer until both pointers meet 

Lets visually see it 





this is how the number looks like  now we add two pointers one at the beginning of the array and another  at the end of the array 



so we are comparing both their values  if they are same then  we increment the start pointer and decrement the endpointer   since both pointers meet we exit the loop and returns true that the given number is a palindrome


Time Complexity:

             the time complexity is o(n)  since we are comparing it in one go 

             space complexity is o(n) since we use a character array to store number 



Code





TestCases:


in the above  we can see the test cases with coverage , if you have any doubts or opinions please comment 

Thursday, 9 April 2020

GCP SQL (Postgres) integration with spring boot

here we will see how to connect to a  postgres instance in GCP , using spring boot

add these dependencies in the pom.xml










and add the following properties in application.properties file





and one more thing we have to install SDK and authenticate your account

https://cloud.google.com/sdk/gcloud/reference/auth/login. ,  follow this link to authenticate. now we are connected to GCP