Solved

wordappend challenge

Posted on 2016-09-30
8
244 Views
Last Modified: 2016-10-05
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
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
8 Comments
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 250 total points
ID: 41824745
Its not related to a index of the string, but the count of the string. If the string count is 2, 4, 6, 8 then and then it will be appended, otherwise not.

Examples:
["a", "", "a"] = 2 (as "a" is appeared 2 times) - final result "a".
["a", "a", ""] = 2 (as "a" is appeared 2 times) - final result "a".
["a", "a", "", "a"] = 3 (as "a" is appeared 3 times) - final result "a".
["a", "a", "a", "a", ""] = 4 (as "a" is appeared 4 times) - final result "aa".

Hope it explains.
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 125 total points
ID: 41826364
In fact you could count the number of occurrences of "a".
In the string to return you need to have (total number of "a" / 2) times "a".
No "a" found ==> ""
Once "a" found ==> ""
Twice "a" found ==> return "a"
3x "a" found ==> return "a"
4x "a" found ==> return "aa"
5x "a" found ==> return "aa"
6x "a" found ==> return "aaa"
...
0
 
LVL 7

Author Comment

by:gudii9
ID: 41828681
is it like  return (number of times a found divided by 2 )number of times of a??
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 7

Author Comment

by:gudii9
ID: 41828684

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
0
 
LVL 7

Author Comment

by:gudii9
ID: 41828761
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
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 125 total points
ID: 41829073
String result="";
Error:      result= map.keySet();
               ^^^^^^^^^^^^
 Type mismatch: cannot convert from Set<String> to String

Just trying to help with your compiler error. When you get an error like that, check the api and confirm that your types and number of args match accordingly.
"Set<K>   keySet()    Returns a Set view of the keys contained in this map."
https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41829416
[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?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41830243
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?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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