Click Here to go back to the homepage.

Prva Solution:


import java.util.*;

public class Kattis {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String[] lenwid = sc.nextLine().split(" ");
    int len = Integer.parseInt(lenwid[0]);
    int wid = Integer.parseInt(lenwid[1]);
    String[] crossword = new String[len];
    for (int i = 0; i < len; i++) {
      crossword[i] = sc.nextLine();
    }
    HashSet<String> words = new HashSet<>();
    StringBuilder[] vertical = new StringBuilder[wid];
    for (int i = 0; i < wid; i++) {
      vertical[i] = new StringBuilder();
    }
    for (int i = 0; i < len; i++) {
      words.addAll(Arrays.asList(Arrays.stream(crossword[i].split("#")).filter((word) -> word.length() > 1).toArray(String[]::new)));
      for (int j = 0; j < wid; j++) {
        if (crossword[i].charAt(j) != '#') {
          vertical[j].append(crossword[i].charAt(j));
        }
        if (crossword[i].charAt(j) == '#' || i == len - 1) {
          if (vertical[j].toString().length() > 1) {
            words.add(vertical[j].toString());
          }
          vertical[j] = new StringBuilder();
        }
      }
    }
    words.stream().sorted().limit(1).forEach(System.out::println);
  }

}