Solved

pairs challenge

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 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

25 Experts available now in Live!

Get 1:1 Help Now