MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / canConstruct

Method canConstruct

Java/ransom-note.java:9–33  ·  view source on GitHub ↗
(String ransomNote, String magazine)

Source from the content-addressed store, hash-verified

7*/
8class Solution {
9 public boolean canConstruct(String ransomNote, String magazine) {
10
11 if (ransomNote.length() > magazine.length())
12 return false;
13
14 /* Logic : character count in ransomNote should be equal to OR less than magazine character count */
15
16 int [] ransomNoteChCount = new int[26]; // lower case characters are only 26
17
18 for (int i=0;i<ransomNote.length();i++){
19 ransomNoteChCount[ransomNote.charAt(i) - 'a']++;
20 }
21
22 for (int i=0;i<magazine.length();i++){
23 ransomNoteChCount[magazine.charAt(i) - 'a']--;
24 }
25
26 for(int i=0;i<26;i++){
27 if (ransomNoteChCount[i] > 0){
28 return false;
29 }
30 }
31
32 return true;
33 }
34}

Callers

nothing calls this directly

Calls 1

lengthMethod · 0.80

Tested by

no test coverage detected