Skip to main content

Problems and solution

Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Input: nums = [2,7,11,15], target = 9 Output: [0,1]

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> numMap =  new HashMap<>();

        for(int i = 0 ; i < nums.length;i++ ){
           int complement = target-nums[i];
           if(numMap.containsKey(complement)){
            return new int[]{numMap.get(complement),i};
           }
           numMap.put(nums[i],i);

        }
        return null;

    }
}

83. Remove Duplicates from Sorted List

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Input: head = [1,1,2] Output: [1,2]

/**
 * 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 deleteDuplicates(ListNode head) {
        ListNode res = head;
        while(head != null && head.next != null){
            if(head.val == head.next.val){
                head.next = head.next.next;
            }
            else{
                head = head.next;
            }
        }
        return res;
    }
}

Substring with concatenation of All words

You are given a string s and an array of strings words. All the strings of words are of the same length.

A concatenated string is a string that exactly contains all the strings of any permutation of words concatenated.

For example, if words = ["ab","cd","ef"], then "abcdef", "abefcd", "cdabef", "cdefab", "efabcd", and "efcdab" are all concatenated strings. "acdbef" is not a concatenated string because it is not the concatenation of any permutation of words. Return an array of the starting indices of all the concatenated substrings in s. You can return the answer in any order.

Example 1:

Input: s = "barfoothefoobarman", words = ["foo","bar"]

Output: [0,9]

Explanation:

The substring starting at 0 is "barfoo". It is the concatenation of ["bar","foo"] which is a permutation of words. The substring starting at 9 is "foobar". It is the concatenation of ["foo","bar"] which is a permutation of words.

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
       List<Integer> ans = new ArrayList<Integer>();
       Map<String,Integer> map = new HashMap<String,Integer>();
       int wordCount = words.length;
       int wordlength = words[0].length();
       for(String word : words){
        map.put(word,map.getOrDefault(word,0)+1);
       }
       for (int i = 0; i<= s.length() - wordlength*wordCount ;i++){
            Map<String,Integer> copymap = new HashMap<String,Integer>(map);
            for(int j = 0; j <wordCount;j++ ){
                String subString = s.substring(i+j*wordlength,i+(j+1)*wordlength);
                if(copymap.containsKey(subString)){
                    int count = copymap.get(subString);
                    if (count == 1){
                        copymap.remove(subString);
                    }else{
                        copymap.put(subString,count-1);
                    }
                    if (copymap.isEmpty()){
                        ans.add(i);
                        break;
                    }
                }
                else{
                    break;
                }
            }
       }
       return ans;
    }
}

Optimised code

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
       List<Integer> ans = new ArrayList<>();
       int wordCount = words.length;
       int wordLength = words[0].length();
       int stringLength = s.length();
       Map<String,Integer> origMap = new HashMap<String,Integer>();
       for(String word : words){
        origMap.put(word,origMap.getOrDefault(word,0)+1);
       }
       for(int offset = 0; offset < wordLength; offset++){
        Map<String,Integer> currMap = new HashMap<String,Integer>();
        int start = offset;
        int count = 0; 
        for(int end = offset; (end + wordLength) <= stringLength; end += wordLength){
            String currWord = s.substring(end,end + wordLength);
            if(origMap.containsKey(currWord)){
                currMap.put(currWord,currMap.getOrDefault(currWord,0)+1);
                count += 1;
                while(currMap.get(currWord)>origMap.get(currWord)){
                    String startword = s.substring(start,start + wordLength);
                    currMap.put(startword,currMap.get(startword)-1);
                    start +=wordLength;
                    count-=1;
                }
                if(wordCount == count){
                    ans.add(start);
                }
            }
            else{
                count=0;
                currMap.clear();
                start = end+ wordLength;
            }
        }
       }
       return ans;
    }
}