Skip to main content
2 of 3
deleted 67 characters in body
Heslacher
  • 51k
  • 5
  • 83
  • 177

Problems / Bugs

  • The following calls will all throw an ArrayIndexOutOfBoundsException

      String document = "a b c d";
      System.out.println(Solution.solution(document, new String[]{"g"}));
      System.out.println(Solution.solution(document, new String[]{"g", "a", "s", "s"}));
      System.out.println(Solution.solution(document, new String[]{"a", "b" ,"c", "d"}));
    

because of

for (int i = shortestSnippetStart; i <= shortestSnippetEnd; i++) {

Assuming you have fixed the above

The above calls will return

a b c d
a b c d
a b c

  • The following will throw an exception if snippet.length==0
snippet.deleteCharAt(snippet.length()-1);  

General

  • you shouldn't do string concatenation inside StringBuilder.append() method. The append() method returns a StringBuilderso you should just stack the calls like

      snippet.append(words[i])
             .append(" ");
    
  • you shouldn't declare multiple variables in a single line, because this removes readability of the code.

  • give your conditions/variables the possibility to breathe

for(int i = shortestSnippetStart; i<=shortestSnippetEnd; i++){  

should be

    for(int i = shortestSnippetStart; i <= shortestSnippetEnd; i++) {  
Heslacher
  • 51k
  • 5
  • 83
  • 177