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

wordappend challenge

Hi,

I am working on below chalenge

http://codingbat.com/prob/p103593

Loop over the given array of strings to build a result string like this: when a string appears the 2nd, 4th, 6th, etc. time in the array, append the string to the result. Return the empty string if no string appears a 2nd time.

wordAppend(["a", "b", "a"]) → "a"
wordAppend(["a", "b", "a", "c", "a", "d", "a"]) → "aa"
wordAppend(["a", "", "a"]) → "a"


wordAppend(["a", "b", "a"]) → "a"

i expected aa for above as a is at index 2.

please advise
JavaJava EEProgrammingProgramming Languages-OtherSystem Programming

Avatar of undefined
Last Comment
gudii9

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Am P

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.
SOLUTION
zzynx

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
is it like  return (number of times a found divided by 2 )number of times of a??
gudii9

ASKER

Loop over the given array of strings to build a result string like this: when a string appears the 2nd, 4th, 6th, etc. time in the array, append the string to the result. Return the empty string if no string appears a 2nd time.

what it mean by appears at 4th 6th etc??bit confusing description
gudii9

ASKER
i got meaning i think
public String wordAppend(String[] strings) {
  
  
 // public Map<String, Integer> wordCount(String[] strings) {
  Map<String, Integer> map=new TreeMap();
  String result="";
  for(int i=0;i<strings.length;i++){
   // String buffer=strings[i]
  String test=strings[i];
  if(map.containsKey(test)){
  int count=map.get(test);
  map.put(test,count+1);
  }
  
  else{
  map.put(test,1);
  }

  result= map.keySet();
  }
  return result;


}

Open in new window


tried as above and getting errors
Error:      result= map.keySet();
              ^^^^^^^^^^^^
Type mismatch: cannot convert from Set<String> to String
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
zzynx

[1]
is it like  return (number of times a found divided by 2 )number of times of a??
That's what I said, yes.

[2]
what it mean by appears at 4th 6th etc??bit confusing description
A 2nd, 4th, 6th occurrence of the string "a"

[3]
The code in your last post is badly indented

[4]
The method keySet() returns a Set of Strings (the keys of the map), so you can't assign that to a String variable.

[5]
 Map<String, Integer> map=new TreeMap();

Open in new window

Do you have a good reason for using a TreeMap?
gudii9

ASKER
public String wordAppend(String[] strings) {
  
 Map<String, Integer> test    = new HashMap<String, Integer>();
 String               res= "";
  
 for (int i = 0; i < strings.length; i++) {
    
   String key = strings[i];
    
   if (test.containsKey(key)) {
     int val = test.get(key);
     val++;
     if (val % 2 == 0) {
      res = res+key;
     }
     test.put(key, val);
   } else {
     test.put(key, 1);
   }
    
 }
  
 return res;
}

Open in new window


above pass all tests.
Basically if map does not contain key adding 1 as value.otherwise we are getting the value and if it is multiple of 2 adding any improvements or alternate approaches?