Skip to main content

Command Palette

Search for a command to run...

Day 25 of LeetCode Challenge

Published
2 min readView as Markdown
Day 25 of LeetCode Challenge
T

Cloud and DevOps Engineer with hands-on expertise in AWS, CI/CD pipelines, Docker, Kubernetes, and Monitoring tools. Adept at building and automating scalable, fault-tolerant cloud infrastructures, and consistently improving system performance, security, and reliability in dynamic environments.

Problem 1: Longest Palindrome

Link to the problem: https://leetcode.com/problems/longest-palindrome/

class Solution {
    public int longestPalindrome(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for(char ch:s.toCharArray()){
            if(map.containsKey(ch))
                map.put(ch, map.get(ch)+1);
            else
                map.put(ch, 1);
        }
        boolean bool = false;
        int ans = 0;
        for(Map.Entry e:map.entrySet()){
            if(((Integer) e.getValue()).intValue()%2==0)
                ans+=((Integer) e.getValue()).intValue();
            else if(((Integer)e.getValue()).intValue()==1)
                bool = true;
            else{
                ans+=((Integer)e.getValue()).intValue()-1;
                bool = true;
            }
        }
        return bool?ans+1:ans;
    }
}

Problem 2: Lowest Common Ancestor of a BST

Link to the problem: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        while(root!=null){
            if (p.val > root.val && q.val > root.val) {
                root = root.right;
            } else if (p.val < root.val && q.val < root.val) {
                root = root.left;
            } else {
                return root;
            }
        }
        return null;
    }
}

Problem 3: Add Binary

Link to the problem: https://leetcode.com/problems/add-binary/

class Solution {
    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        int i = a.length() - 1;
        int j = b.length() - 1;

        while (i >= 0 || j >= 0 || carry == 1) {
            if (i >= 0)
                carry += a.charAt(i--) - '0';
            if (j >= 0)
                carry += b.charAt(j--) - '0';
            sb.append(carry % 2);
            carry /= 2;
        }
        return sb.reverse().toString();
    }
}

Problem 4: Find the Original Typed String I

Link to the problem: https://leetcode.com/problems/find-the-original-typed-string-i/

class Solution {
    public int possibleStringCount(String word) {
        List<Character> list = new ArrayList<>();
        int len=0;
        int ans=1;
        for(char ch:word.toCharArray()){
            if(list.isEmpty()){
                list.add(ch);
                len++;
            }else if(ch==list.get(len-1))
                ans++;
            else{
                list.add(ch);
                len++;
            }
        }
        return ans;
    }
}

Problem 5: Number of 1 bits

Link to the problem: https://leetcode.com/problems/number-of-1-bits/

class Solution {
    public int hammingWeight(int n) {
        StringBuilder sb = new StringBuilder();
        while(n>0){
            sb.append(Integer.toString(n%2));
            n /= 2;
        }
        String str = sb.toString();
        int count = 0;
        for(char ch:str.toCharArray()){
            if(ch=='1')
                count++;
        }
        return count;
    }
}

More from this blog

T

Tushar Pant's Blog

178 posts

Welcome to Tushar's Blog! Here, I share my journey in tech, covering DevOps and cloud computing. Explore tutorials, tips, and insights to fuel your learning and growth in technology.