Skip to content

Add solution for "Ransom Note" with unit tests. - #41

Merged
kobukuro merged 1 commit into
mainfrom
feat/ransom-note
Jan 26, 2026
Merged

kobukuro merged 1 commit into
mainfrom
feat/ransom-note

Conversation

@kobukuro

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings January 26, 2026 02:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a LeetCode 383 (“Ransom Note”) solution into the standard src/main/java package layout and introduces corresponding JUnit tests.

Changes:

  • Added RansomNote383 implementation under com.leetcode.easy.
  • Added RansomNote383Test with tests matching the problem’s example cases.
  • Removed the previous top-level src/RansomNote383.java version.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/main/java/com/leetcode/easy/RansomNote383.java Introduces the packaged solution implementation (hash-map frequency counting).
src/test/java/com/leetcode/easy/RansomNote383Test.java Adds JUnit 5 tests for the main example scenarios.
src/RansomNote383.java Removes the older un-packaged implementation to avoid duplication/confusion.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +13 to +19
* Time Complexity: O(m)
* - Early return when m < n takes O(1)
* - Building magazine character frequency map: O(m)
* - Building ransom note character frequency map: O(n)
* - Validating character counts by iterating ransom note map: O(n)
* - Total: O(m) + O(n) + O(n)
* - Since we only proceed when m ≥ n, we can simplify: O(m) + O(m) + O(m) = O(m)

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The time-complexity breakdown in this Javadoc is mathematically inconsistent with what the code does: iterating countR.entrySet() is O(k) (unique chars), not O(n), and the “Total: O(m) + O(n) + O(n)” / “simplify to … O(m) + O(m) + O(m)” lines don’t match the earlier bullets. Please correct the analysis to reflect the actual terms (e.g., O(m + n + k) and the simplification when m ≥ n).

Suggested change
* Time Complexity: O(m)
* - Early return when m < n takes O(1)
* - Building magazine character frequency map: O(m)
* - Building ransom note character frequency map: O(n)
* - Validating character counts by iterating ransom note map: O(n)
* - Total: O(m) + O(n) + O(n)
* - Since we only proceed when m ≥ n, we can simplify: O(m) + O(m) + O(m) = O(m)
* Time Complexity: O(m + n + k)
* - Early return when m < n takes O(1)
* - Building magazine character frequency map: O(m)
* - Building ransom note character frequency map: O(n)
* - Validating character counts by iterating ransom note map: O(k)
* - Total: O(m + n + k)
* - Since we only proceed when m ≥ n and k ≤ n, we can simplify: O(m + n + k) ⊆ O(m + m + m) = O(m)

Copilot uses AI. Check for mistakes.
Comment on lines +13 to +41
* Time Complexity: O(m)
* - Early return when m < n takes O(1)
* - Building magazine character frequency map: O(m)
* - Building ransom note character frequency map: O(n)
* - Validating character counts by iterating ransom note map: O(n)
* - Total: O(m) + O(n) + O(n)
* - Since we only proceed when m ≥ n, we can simplify: O(m) + O(m) + O(m) = O(m)
* <p>
* Space Complexity: O(k) or O(1)
* - Two hash maps store character frequencies, each with at most k unique characters
* - Since k ≤ 26 (constant), can be considered O(1) space
*/
public boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length()) {
return false;
}
Map<Character, Integer> countR = new HashMap<>();
for (char c : ransomNote.toCharArray()) {
countR.put(c, countR.getOrDefault(c, 0) + 1);
}
Map<Character, Integer> countM = new HashMap<>();
for (char c : magazine.toCharArray()) {
countM.put(c, countM.getOrDefault(c, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : countR.entrySet()) {
if (countM.getOrDefault(entry.getKey(), 0) < entry.getValue()) {
return false;
}
}

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This builds two separate frequency maps (countR and countM) and does multiple passes over the inputs. Consider using a single counter (e.g., count magazine chars then decrement while iterating ransomNote) to reduce allocations and allow earlier failure.

Suggested change
* Time Complexity: O(m)
* - Early return when m < n takes O(1)
* - Building magazine character frequency map: O(m)
* - Building ransom note character frequency map: O(n)
* - Validating character counts by iterating ransom note map: O(n)
* - Total: O(m) + O(n) + O(n)
* - Since we only proceed when m ≥ n, we can simplify: O(m) + O(m) + O(m) = O(m)
* <p>
* Space Complexity: O(k) or O(1)
* - Two hash maps store character frequencies, each with at most k unique characters
* - Since k ≤ 26 (constant), can be considered O(1) space
*/
public boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length()) {
return false;
}
Map<Character, Integer> countR = new HashMap<>();
for (char c : ransomNote.toCharArray()) {
countR.put(c, countR.getOrDefault(c, 0) + 1);
}
Map<Character, Integer> countM = new HashMap<>();
for (char c : magazine.toCharArray()) {
countM.put(c, countM.getOrDefault(c, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : countR.entrySet()) {
if (countM.getOrDefault(entry.getKey(), 0) < entry.getValue()) {
return false;
}
}
* Time Complexity: O(m + n)
* - Early return when m < n takes O(1)
* - Building magazine character frequency counter: O(m)
* - Validating character counts while iterating ransom note: O(n), with possible early exit
* <p>
* Space Complexity: O(k) or O(1)
* - Single counter storing character frequencies, with at most k unique characters
* - Since k ≤ 26 (constant), can be considered O(1) space
*/
public boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length()) {
return false;
}
int[] charCounts = new int[26];
for (char c : magazine.toCharArray()) {
charCounts[c - 'a']++;
}
for (char c : ransomNote.toCharArray()) {
int index = c - 'a';
charCounts[index]--;
if (charCounts[index] < 0) {
return false;
}
}

Copilot uses AI. Check for mistakes.
@kobukuro
kobukuro merged commit c999f5c into main Jan 26, 2026
7 checks passed
@kobukuro
kobukuro deleted the feat/ransom-note branch January 26, 2026 02:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants