Created
March 11, 2023 12:48
-
-
Save VallarasuS/5e3f1913d0c623914241862100c923ab to your computer and use it in GitHub Desktop.
383. Ransom Note - https://leetcode.com/problems/ransom-note/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean canConstruct(String ransomNote, String magazine) { | |
HashMap<Character,Integer> charMap = new HashMap<Character, Integer>(); | |
for(char c : magazine.toCharArray()) { | |
if(charMap.containsKey(c)) { | |
int frequency = charMap.get(c); | |
charMap.put(c, frequency + 1); | |
} | |
else { | |
charMap.put(c, 1); | |
} | |
} | |
for (char c: ransomNote.toCharArray()) { | |
if(charMap.containsKey(c)) { | |
int frequency = charMap.get(c); | |
if(frequency <= 0) { | |
return false; | |
} | |
charMap.put(c, frequency - 1); | |
} | |
else { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment