Solved

pairs challenge

Posted on 2016-09-30
5
130 Views
Last Modified: 2016-10-03
Hi,

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

Given an array of non-empty strings, create and return a Map<String, String> as follows: for each string add its first character as a key with its last character as the value.

pairs(["code", "bug"]) → {"b": "g", "c": "e"}
pairs(["man", "moon", "main"]) → {"m": "n"}
pairs(["man", "moon", "good", "night"]) → {"g": "d", "n": "t", "m": "n"}
 
Not clear on  the description.
please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Expert Comment

by:tel2
ID: 41824332
Hi gudii9,

> "Not clear on  the description.
> please advise"


Unless you tell us which parts of the description you are not clear on, it's a bit hard for us to know how to advise you.  Do you expect us to rewrite the entire description and hope that you understand our version?

So, instead of making this a guessing game for us, please be specific.  We're expert programmers, not expert mind readers.

Thanks.
tel2
1
 
LVL 7

Author Comment

by:gudii9
ID: 41826700
no duplicates?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41826702
pairs(["man", "moon", "main"]) → {"m": "n"}

above has only one m and n not 3 times for man, moon, main
0
 
LVL 7

Author Comment

by:gudii9
ID: 41826763
public Map < String, String > pairs(String[] strings) {
    Map < String, String > map = new HashMap();
    for (int i = 0; i < strings.length; i++) {
        String test = strings[i];
        String first = String.valueOf(test.charAt(0));
        String last = String.valueOf(test.charAt(test.length() - 1));
        map.put(first, last);
        // map.put("1","last");

    }
    return map;
}

Open in new window


looks like map removed all duplicate keys by itself/
above passed all tests. any improvement or alternate approaches?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 41826809
>> looks like map removed all duplicate keys by itself

You seem a little surprised by this. I suggest that you take time to look at the MAP API in the challenge link:
"map.put(key, value) -- stores a new key/value pair in the map. Overwrites any existing value for that key."

From this statement, you can infer that all keys in the MAP are unique.

Or, whenever encountering a new container, go to more official sources:
https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
public interface Map<K,V>

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Simple Linear Regression
Suggested Courses

622 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