Thursday, June 26, 2014

ImageDithering

Problem:

TopCoder Problem Statement - ImageDithering

Overview:

Count the number of times any letter in "dithered" appear in "screen".

Java Source:
01: /*
02: TopCoder
03: Single Round Match: 145
04: Division: 2
05: Level: 1
06: Points: 250
07: Description: http://community.topcoder.com/stat?c=problem_statement&pm=1728
08:  */
09:
10: import java.util.HashSet;
11: import java.util.Set;
12:
13: public class ImageDithering {
14:
15:     public int count(String dithered, String[] screen) {
16:
17:         int count = 0;
18:
19:         char[] dColors = dithered.toCharArray();
20:         Set dSet = new HashSet<>();
21:
22:         for (char dColor : dColors) {
23:             dSet.add(dColor);
24:         }
25:
26:         for (String row : screen) {
27:             for (int i = 0; i < row.length(); i++) {
28:                 if (dSet.contains(row.charAt(i))) {
29:                     count++;
30:                 }
31:             }
32:         }
33:
34:         return count;
35:     }
36: }
Notes:

In lines 14-18, I create a Hash set to hold all of the characters in "dithered".

Then in lines 24-30, I loop through the rows and then columns in screen, checking to see if each character is in that set. If it is, increment count.

No comments:

Post a Comment