Solved

pairs challenge

Posted on 2016-09-30
5
101 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 64
add projects t working set in maven 2 44
spring maven example issues 3 44
import as existing maven project 3 41
A short article about a problem I had getting the GPS LocationListener working.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

730 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