HashMap

HashMap

Ransom Note Problem

Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.

Each letter in magazine can only be used once in ransomNote.

Input: ransomNote = "aa", magazine = "ab"
Output: false

public class RansomNote{
    public static boolean valid(String ransom, String magazine){
        Map<Character, Integer> MagazineCount = new HashMap<>();

        for(char ch : magazine.toCharArray()){
            MagazineCount.put(ch, MagazineCount.getOrDefault(ch, 0) + 1);
        }

        for(char ch : ransom.toCharArray()){
            if(!MagazineCount.containsKey(ch) || MagazineCount.get(ch) == 0){
                 return false;
            }
        }
        return true;
    }
}

Isomorphic Strings

Given two strings s and t, determine if they are isomorphic.

Two strings s and t are isomorphic if the characters in s can be replaced to get t

Input: s = "egg", t = "add"
Output: true
package Hashmap;

import java.util.HashMap;

public class IsomorphicStrings {
    public static void main(String[] args) {
        String s = "egg";
        String t = "rlf";
        System.out.println(isIsomorphic(s,t));

    }

    public static boolean match(String s, String t){
        if(s.length() != t.length()){
            return false;
        }

        HashMap<Character, Character> mp = new HashMap<>();

        for(int i = 0; i < s.length(); i++){
            if(mp.containsKey(s.charAt(i)) && mp.get(s.charAt(i)) != t.charAt(i)){
                return false;
            }else {
                mp.put(s.charAt(i), t.charAt(i));
            }
        }
        return true;
    }

    public static boolean isIsomorphic(String s, String t){
        return match(s,t) && match(t,s);
    }
}

Valid Anagram

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Input: s = "anagram", t = "nagaram"
Output: true

package Hashmap;

import java.util.HashMap;

public class ValidAnagram {
    public static void main(String[] args) {
        String s = "ate";
        String t = "eae";
        System.out.println(isAnagram(s,t));
    }

    public static boolean isAnagram(String s, String t){
        HashMap<Character, Integer> mp = new HashMap<>();

        for (int i = 0; i < s.length(); i++) {
            mp.put(s.charAt(i),mp.getOrDefault(s.charAt(i),0) + 1);
        }

        for (int i = 0; i < t.length(); i++) {
            mp.put(t.charAt(i), mp.getOrDefault(t.charAt(i), 0) - 1);
        }

        for (int x : mp.values()){
            if( x!= 0){
                return false;
            }
        }
        return true;
    }
}

Word Pattern

Given a pattern and a string s, find if s follows the same pattern.

Input: pattern = "abba", s = "dog cat cat dog"
Output: true

package Hashmap;

import java.util.HashMap;

public class WordPattern {
    public static void main(String[] args) {

    }

    public static boolean wordPattern(String pattern, String s) {
        String[] words = s.split(" ");

        if(pattern.length() != words.length){
            return false;
        }

        HashMap<Character, String> CharToWord = new HashMap<>();
        HashMap<String, Character> WordToChar = new HashMap<>();

        for (int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            String word = words[i];

            if(!CharToWord.containsKey(c)){
                CharToWord.put(c,word);
            }

            if(!WordToChar.containsKey(word)){
                WordToChar.put(word,c);
            }

            if(!CharToWord.get(c).equals(word) || !WordToChar.get(word).equals(c)){
                return false;
            }
        }
        return true;
    }
}

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]

package Hashmap;

import java.util.HashMap;

public class TwoSum {
    public static void main(String[] args) {
        int[] arr = {2, 7, 11, 5};
        int target = 9;
        int[] ans = twoSum(arr, target);

        for (int i = 0; i < ans.length; i++) {
            System.out.print(ans[i]+" ");
        }

    }

    public static int[] twoSum(int[] arr, int target){
        HashMap<Integer, Integer> mp = new HashMap<>();

        for (int i = 0; i < arr.length; i++) {
            int sum = target - arr[i];

            if(mp.containsKey(sum)){
                return new int[]{mp.get(sum), i};
            }
            mp.put(arr[i], i);
        }
        return new int[] {-1, -1};

    }
}

Group Anagram

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

package Hashmap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class GroupAnagram {
    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result = grpAnagram(strs);

        for (List<String> group : result) {
            System.out.println(group);
        }

    }

    public static List<List<String>> grpAnagram(String[] strs){
        HashMap<String, List<String>> mp = new HashMap<>();

        for (int i = 0; i < strs.length; i++) {
            String word = strs[i];
            char[] chars = word.toCharArray();
            Arrays.sort(chars);
            String SortedWord = new String(chars);

            if(!mp.containsKey(SortedWord)){
                mp.put(SortedWord, new ArrayList<>());
            }
            mp.get(SortedWord).add(word);
        }
        return new ArrayList<>(mp.values());
    }
}

Did you find this article valuable?

Support Reuben's blog by becoming a sponsor. Any amount is appreciated!