Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1853108

Browse files
committed
Ransom note done
1 parent bcdc91a commit 1853108

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.leetcode.strings;
2+
3+
/**
4+
* Problem: https://leetcode.com/problems/ransom-note/
5+
*
6+
* @author rampatra
7+
* @since 2019年04月19日
8+
*/
9+
public class RansomNote {
10+
11+
/**
12+
* Runtime: <a href="https://leetcode.com/submissions/detail/223597898/">4 ms/a>.
13+
*
14+
* @param ransomNote
15+
* @param magazine
16+
* @return
17+
*/
18+
public static boolean canConstruct(String ransomNote, String magazine) {
19+
char[] charCount = new char[26];
20+
21+
for (int i = 0; i < magazine.length(); i++) {
22+
charCount[magazine.charAt(i) - 'a']++;
23+
}
24+
25+
for (int i = 0; i < ransomNote.length(); i++) {
26+
if (charCount[ransomNote.charAt(i) - 'a']-- == 0) {
27+
return false;
28+
}
29+
}
30+
return true;
31+
}
32+
33+
public static void main(String[] args) {
34+
System.out.println(canConstruct("", ""));
35+
System.out.println(canConstruct("a", "a"));
36+
System.out.println(canConstruct("ab", "ab"));
37+
System.out.println(canConstruct("aab", "ab"));
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /