Solved

pairs challenge

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
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.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

746 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

12 Experts available now in Live!

Get 1:1 Help Now