Solved

word0 challenge

Posted on 2016-09-29
3
56 Views
Last Modified: 2016-10-04
Hi,

I am working on below challenge.
http://codingbat.com/prob/p152303

Given an array of strings, return a Map<String, Integer> containing a key for every different string in the array, always with the value 0. For example the string "hello" makes the pair "hello":0. We'll do more complicated counting later, but for this problem the value is simply 0.

word0(["a", "b", "a", "b"]) → {"b": 0, "a": 0}
word0(["a", "b", "a", "c", "b"]) → {"b": 0, "c": 0, "a": 0}
word0(["c", "b", "a"]) → {"b": 0, "c": 0, "a": 0}

not clean on above description . please advise
0
Comment
Question by:gudii9
  • 2
3 Comments
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
Map<String, Integer>
For every unique key (i.e., a string), there is an integer value. In this problem, the integer value is always zero. If the unique key appears more than once in an array, then the map can only have one node for that unique key.

(["a", "b", "a", "c", "b"]) -- this array has three unique keys, "a", "b", "c"
The map therefore can only have three nodes, one for each unique key.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
public Map<String, Integer> word0(String[] strings) {
  int len=strings.length;
   Map<String, Integer> map = new HashMap<String, Integer>();
 for(int i=0;i<len;i++){
   
 map.put(strings[i],0);
 }
  return map;
}

Open in new window

above passes all the tests. any improvements or alternate approaches?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
Comment Utility
At work, when code is not indented, we do not even review it. Indentation is that important. In fact, if you learn Python, if  you don't get the indentation exactly right, then you can get very subtle errors. (I think that is overkill in enforcing good coding standards, but now Python is gaining in popularity, so keep that in mind.)

Since you only have a few lines, I was willing to review it. Try to indent in the future. Others have asked you to do that. Don't forget, you can hit the "Preview" button to see how your post with code looks.

Your code is fine, but the code from their Show Solution button is even better:
public Map<String, Integer> word0(String[] strings) {
   Map<String, Integer> map = new HashMap();
   for (String s:strings) {  // no need to compute len as you did since compiler knows the array length
      map.put(s, 0);
   }
   return map;
}

Open in new window


The for-statement says, assign s, a String, to each element in the strings array, one at a time, and use that s in the body of the for-loop. It is just as you did, but much cleaner looking since their solution only has three variables: strings, map and s. It is not just cleaner, but also less error prone, since you have to manipulate you counter variable, i.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now