fork download
  1. // package whatever; // don't place package name!
  2.  
  3. import java.io.*;
  4. import java.util.Set;
  5. import java.util.HashSet;
  6.  
  7. /*
  8. Write a program that parses a sentence and replaces each word with the following:
  9. 1) The first letter of the word
  10. 2) The number of distinct characters between first and last character
  11. 3) The last letter of the word.
  12. For example, Smooth would become S3h.
  13. Words are separated by spaces or non-alphabetic characters and these separators should be maintained in their original form and location in the answer.
  14. A few of the things we will be looking at is accuracy, efficiency, solution completeness.
  15. */
  16. class MyCode {
  17. public static String wordParser(String input) {
  18. StringBuilder sentence = new StringBuilder();
  19. StringBuilder current = new StringBuilder();
  20. char value;
  21. for(int i=0; i<input.length(); i++){
  22. value = input.charAt(i);
  23. if(!Character.isAlphabetic(value)){
  24. sentence.append(converterWord(current)).append(value);
  25. current = new StringBuilder();
  26. }else{
  27. current.append(value);
  28. }
  29. }
  30. return current.length() > 0 ? sentence.append(converterWord(current)).toString() : sentence.toString();
  31. }
  32.  
  33. private static CharSequence converterWord(StringBuilder word){
  34. if(word.length() > 2){
  35. Set<Character> characters = new HashSet<>();
  36. for(int i=1; i<word.length() - 1; i++){
  37. characters.add(word.charAt(i));
  38. }
  39. return String.valueOf(word.charAt(0)) + characters.size() + word.charAt(word.length() - 1);
  40. }
  41. return word;
  42. }
  43.  
  44. public static void main (String[] args) {
  45. String output = wordParser("Creativity is thinking-up new things. Innovation is doing new things!");
  46. System.out.println(output);
  47. // expected: C6y is t4g-up n1w t4s. I6n is d3g n1w t4s!
  48. String output2 = wordParser("Smooth");
  49. System.out.println(output2);
  50. }
  51. }
Success #stdin #stdout 0.16s 50660KB
stdin
Standard input is empty
stdout
C6y is t4g-up n1w t4s. I6n is d3g n1w t4s!
S3h