Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pairs challenge

Posted on 2016-09-30
5
Medium Priority
?
155 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Starting up a Project
Suggested Courses

715 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