Day 4 of LeetCode Challenge

Tushar PantTushar Pant
3 min read

Problem 1: Minimum Number of Operations to Make Word K-Periodic

link to the problem

class Solution {
    public int minimumOperationsToMakeKPeriodic(String word, int k) {
        int maxOfAllFrequencies = 0;
        Map<String, Integer> m = new HashMap<>();

        for (int i = 0; i < word.length(); i += k) {
            String sub = word.substring(i, Math.min(i + k, word.length()));
            m.put(sub, m.getOrDefault(sub, 0) + 1);
        }
        for (int freq : m.values()) 
            maxOfAllFrequencies = Math.max(maxOfAllFrequencies, freq);

        return word.length() / k - maxOfAllFrequencies;
    }
}

Problem 2: Total Distance Traveled

link to the problem

class Solution {
    public int distanceTraveled(int mainTank, int additionalTank) {
        int num = (mainTank-1)/4>additionalTank?additionalTank:(mainTank-1)/4;
        return (mainTank + num) * 10;
    }
}

Problem 3: Longest Common Prefix

link to the problem

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) return "";

        String pref = strs[0];
        int prefLen = pref.length();

        for (int i = 1; i < strs.length; i++) {
            String s = strs[i];
            while (prefLen > s.length() || !pref.equals(s.substring(0, prefLen))) {
                prefLen--;
                if (prefLen == 0) {
                    return "";
                }
                pref = pref.substring(0, prefLen);
            }
        }

        return pref;        
    }
}

Problem 4: Find Subtree Sizes After Changes

link to the problem

class Solution {
    public int[] findSubtreeSizes(int[] parent, String s) {
        int n = parent.length;
        List<List<Integer>> adj = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            adj.add(new ArrayList<>());
        }

        for (int i = 1; i < n; i++) {
            adj.get(parent[i]).add(i);
        }

        int[] newParent = Arrays.copyOf(parent, n);
        Map<Character, Integer> lastSeen = new HashMap<>();
        dfsAdjustParent(0, s, adj, lastSeen, newParent);

        List<List<Integer>> finalAdj = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            finalAdj.add(new ArrayList<>());
        }
        for (int i = 1; i < n; i++) {
            finalAdj.get(newParent[i]).add(i);
        }

        int[] answer = new int[n];
        dfsCountSubtreeSizes(0, finalAdj, answer);

        return answer;
    }

    public void dfsAdjustParent(int node, String s, List<List<Integer>> adj, Map<Character, Integer> lastSeen, int[] newParent) {
        char c = s.charAt(node);
        Integer prev = lastSeen.get(c);
        if (prev != null && prev != newParent[node]) {
            newParent[node] = prev;
        }

        Integer originalParent = lastSeen.put(c, node);
        for (int child : adj.get(node)) {
            dfsAdjustParent(child, s, adj, lastSeen, newParent);
        }
        if (originalParent == null) {
            lastSeen.remove(c);
        } else {
            lastSeen.put(c, originalParent);
        }
    }

    public int dfsCountSubtreeSizes(int node, List<List<Integer>> adj, int[] answer) {
        int size = 1;
        for (int child : adj.get(node)) {
            size += dfsCountSubtreeSizes(child, adj, answer);
        }
        answer[node] = size;
        return size;
    }
}

Problem 5: Middle of the Linked List

link to the problem

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        int len=1;
        ListNode temp = new ListNode(head.val, head.next);
        while(temp.next!=null){
            temp = temp.next; 
            len++;
        }
        len=(len/2)+1;
        System.out.println(len);
        while(len>1){
            head = head.next;
            len--;
        }
        return head;
    }
}
0
Subscribe to my newsletter

Read articles from Tushar Pant directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tushar Pant
Tushar Pant