Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

firstChar challenge

Posted on 2016-09-30
13
Medium Priority
?
222 Views
Last Modified: 2016-10-06
Hi,

I am working on below challenge.

http://codingbat.com/prob/p168493

Given an array of non-empty strings, return a Map<String, String> with a key for every different first character seen, with the value of all the strings starting with that character appended together in the order they appear in the array.

firstChar(["salt", "tea", "soda", "toast"]) → {"t": "teatoast", "s": "saltsoda"}
firstChar(["aa", "bb", "cc", "aAA", "cCC", "d"]) → {"d": "d", "b": "bb", "c": "cccCC", "a": "aaaAA"}
firstChar([]) → {}

i was not clear on above 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
  • 7
  • 5
13 Comments
 
LVL 12

Expert Comment

by:tel2
ID: 41824327
Hi gudii9,

> "i was not clear on above 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: 41826815

Given an array of non-empty strings, return a Map<String, String> with a key for every different first character seen, with the value of all the strings starting with that character appended together in the order they appear in the array.

firstChar(["salt", "tea", "soda", "toast"]) → {"t": "teatoast", "s": "saltsoda"}

why only character key s taken but not a/l/t in the salt??
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41826847
>> why only character key s taken but not a/l/t in the salt??

No particular reason. These challenges are sometimes testing your algorithm skills. In this case, the question is just testing your MAP and string skills. The question variants could have said take the first two chars for the key unless the third char (if it exists) is 'z', in which case "z" is the string key.

These questions are just exercises to make you stronger. But they may not have any value in professional programming on the job. (But making you stronger is, of course, very valuable - I am not saying that you should stop working these challenges.)
0
Industry Leaders: 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!

 
LVL 7

Author Comment

by:gudii9
ID: 41826968
every different first

i missed first in description
0
 
LVL 7

Author Comment

by:gudii9
ID: 41826988
let me think
0
 
LVL 7

Author Comment

by:gudii9
ID: 41830626
public Map < String, String > firstChar(String[] strings) {

    Map < String, String > map = new HashMap < String, String > ();

    for (int i = 0; i < strings.length; i++) {

        String k = String.valueOf(strings[i].charAt(0));

        if (map.containsKey(k)) {
            String val = map.get(k) + strings[i];
            map.put(k, val);
        } else {
            map.put(k, strings[i]);
        }

    }
    return map;
}

Open in new window


i pass all tests. any improvements or alternate approaches?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41831025
Looks good. Maybe save a few keystrokes:
Map < String, String > map = new HashMap();
0
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 41831142
And, of course, as already explained in other questions, you can change the for-loop so that you do not need the counter, i.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41832124
public Map<String, String> firstChar(String[] strings) {
   
  Map<String, String> map = new HashMap();
   
 // for (int i = 0; i < strings.length; i++) {
 for(String str:strings)
     
    String k = String.valueOf(str.charAt(0));
     
    if (map.containsKey(k)) {
      String val = map.get(k) + strings[i];
      map.put(k, val);
    } else {
      map.put(k, strings[i]);
    }
     
  }
  return map;
}

Open in new window


above gives below error
Compile problems:


Error:      String k = String.valueOf(str.charAt(0));
      ^^^^^^
Syntax error, insert "AssignmentOperator ArrayInitializer" to complete ArrayInitializerAssignement


see Example Code to help with compile problems

please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 41832135
public Map<String, String> firstChar(String[] strings) {
   
  Map<String, String> map = new HashMap();
   
 // for (int i = 0; i < strings.length; i++) {
 for(String str:strings){
     
    String k = String.valueOf(str.charAt(0));
     
    if (map.containsKey(k)) {
      String val = map.get(k) + str;
      map.put(k, val);
    } else {
      map.put(k, str);
    }
     
  }
  return map;
}

Open in new window



i corrected it. missed { and also used strings[i] at one place

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 41832214
>> i corrected it.
Excellent!
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41832229
I am not against temporary variables, which good compilers will optimize away. And, when debugging a problem, temp variables are very helpful. But some projects hate them. So, if you see something like this, it is due to their hatred.
      // String val = map.get(k) + str;
      map.put(k, map.get(k) + str);

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41832315
i see your point
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

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.
A solution for Fortify Path Manipulation.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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 …
Suggested Courses

636 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