Avatar of gudii9
gudii9
Flag for United States of America asked on

firstChar challenge

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
JavaJava EEProgrammingProgramming Languages-OtherSystem Programming

Avatar of undefined
Last Comment
gudii9

8/22/2022 - Mon
tel2

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
gudii9

ASKER

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??
phoffric

>> 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.)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
gudii9

ASKER
every different first

i missed first in description
gudii9

ASKER
let me think
gudii9

ASKER
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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
phoffric

Looks good. Maybe save a few keystrokes:
Map < String, String > map = new HashMap();
ASKER CERTIFIED SOLUTION
phoffric

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
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
gudii9

ASKER
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

Your help has saved me hundreds of hours of internet surfing.
fblack61
phoffric

>> i corrected it.
Excellent!
phoffric

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

gudii9

ASKER
i see your point
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.