Skip to main content

Command Palette

Search for a command to run...

Day 8 of LeetCode Challenge

Published
2 min readView as Markdown
Day 8 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: Find the first occurrence in a string

Link to the problem: https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/

class Solution {
    public int strStr(String haystack, String needle) {
        if(haystack.length()<needle.length())
            return -1;
        if(haystack.length()==needle.length())
            return haystack.equals(needle)?0:-1;
        for(int i=0; i<haystack.length()-needle.length()+1; i++){
            System.out.println(haystack.substring(i, i+needle.length())+" ");
            if(haystack.charAt(i)==needle.charAt(0) && haystack.substring(i, i+needle.length()).equals(needle)){
                System.out.println(haystack.substring(i, i+needle.length())+" ");
                return i;
            }
        }
        return -1;
    }
}

Problem 2: Maximum odd binary number

Link to the problem: https://leetcode.com/problems/maximum-odd-binary-number/description/

class Solution {
    public String maximumOddBinaryNumber(String s) {
        int count1 = 0; int count0 = 0;
        for(char ch : s.toCharArray()){
            if(ch=='1')
                count1++;
            else 
                count0++;
        }
        s="";
        while(count1>1){
            s=s.concat(Character.toString('1'));
            count1--;
        }   
        while(count0>0){
            s=s.concat(Character.toString('0'));
            count0--;
        }
        s=s.concat(Character.toString('1'));
        return s;
    }
}

Problem 3: Rings and Rods

Link to the problem: https://leetcode.com/problems/rings-and-rods/description/

class Solution {
    public int countPoints(String rings) {
        String[] arr = new String[10];
        Arrays.fill(arr, "");
        int i=0;
        int count = 0;
        while(i < rings.length()){
            arr[Integer.parseInt(Character.toString(rings.charAt(i+1)))] = arr[Integer.parseInt(Character.toString(rings.charAt(i+1)))].concat(Character.toString(rings.charAt(i)));
            i+=2;
        }
        for(String str : arr){
            System.out.println(str);
            if(str.contains("B") && str.contains("R") && str.contains("G"))
                count++;
        }
        return count;
    }
}

Problem 4: Verifying an Alien Dictionary

Link to the problem: https://leetcode.com/problems/verifying-an-alien-dictionary/description/

class Solution {
    public boolean isAlienSorted(String[] words, String order) {
        int num = 0;
        for(int i=0; i<words.length-1; i++){
            int min_len = words[i].length()<words[i+1].length()?words[i].length():words[i+1].length();
            if(words[i].substring(0, min_len).equals(words[i+1].substring(0, min_len)) && words[i].length()>words[i+1].length())
                return false;
            for(int j=0; j<min_len; j++){
                if(words[i].charAt(j) != words[i+1].charAt(j)){
                    System.out.println(words[i]+" "+words[i+1]);
                    System.out.println(i+" "+j+" "+(i+1));
                    System.out.println(order.indexOf(words[i].charAt(j))+" "+order.indexOf(words[i+1].charAt(j)));
                    num = order.indexOf(words[i].charAt(j))<order.indexOf(words[i+1].charAt(j))?1:-1;
                }
                if(num==1)
                    break;
                else if(num==-1)
                    return false;
            }
        }
        return true;
    }
}

Problem 5: Unique Morse Code Words

Link to the problem: https://leetcode.com/problems/unique-morse-code-words/description/

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        String[] arr = new String[words.length];
        int len = 0;
        for(String str:words){
            String code = "";
            for(char ch : str.toCharArray())
                code=code.concat(morse[(int)ch-97]);
            boolean bool = true;
            for(int i=0; i<len; i++){
                if(arr[i].equals(code))
                    bool = false;
            }
            if(bool){
                arr[len] = code;
                len++;
            }
        }
        return len;
    }
}

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.