Advertisement
Guest User

Untitled

a guest
Sep 5th, 2013
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4.  
  5. public class wordsAnalizer {
  6.    
  7.     public LinkedList<String> wordsList = new LinkedList<>();
  8.     public ArrayList<String> minUniqueSimbolWords = new ArrayList<String>();
  9.     final int wordsCount = 3;
  10.  
  11.     private void readData() {
  12.         System.out.println("Iveskite " + wordsCount + " zodzius: ");
  13.         Scanner scan = new Scanner(System.in);
  14.         for (int i = 0; i < wordsCount; i++) {
  15.             wordsList.add(scan.nextLine());
  16.         }
  17.  
  18.         scan.close();
  19.     }
  20.  
  21.     private void printResults() {
  22.         if (minUniqueSimbolWords.isEmpty()) {
  23.             System.out.println("not unique words");
  24.             return;
  25.         }
  26.  
  27.         printUniqueSimbolWords();
  28.     }
  29.  
  30.     public void run() {
  31.         readData();
  32.         selectWordsWithMinimumCountOfUniqueCharacters();
  33.         printResults();
  34.     }
  35.  
  36.     private void printUniqueSimbolWords() {
  37.         System.out.println("");
  38.        
  39.         for (String s : minUniqueSimbolWords) {
  40.             System.out.println(s);
  41.         }
  42.     }
  43.  
  44.     private void processWord(String word) {
  45.         if (minUniqueSimbolWords.isEmpty()) {
  46.             minUniqueSimbolWords.add(word);
  47.         } else {
  48.             int count = getUniqueSimbolCount(word.toCharArray());
  49.             addMinUniqueSimbolsCountWord(word, count);
  50.         }
  51.     }
  52.  
  53.     private void selectWordsWithMinimumCountOfUniqueCharacters() {
  54.         for (String word : wordsList) {        
  55.             processWord(word);
  56.         }
  57.     }
  58.  
  59.    
  60.     private void addMinUniqueSimbolsCountWord(String word, int count) {
  61.         int countOfFirstFromList = getUniqueSimbolCount(wordsList.getFirst()
  62.                 .toCharArray());
  63.         if (count < countOfFirstFromList) {
  64.             minUniqueSimbolWords.clear();
  65.             minUniqueSimbolWords.add(word);
  66.  
  67.         } else if (count == countOfFirstFromList) {
  68.             minUniqueSimbolWords.add(word);
  69.         }
  70.  
  71.     }
  72.  
  73.     private int getUniqueSimbolCount(char[] str) {
  74.         ArrayList<Character> lst = new ArrayList<Character>();
  75.         for (char c : str) {
  76.             if (!lst.contains(c)) {
  77.                 lst.add(c);
  78.             }
  79.         }
  80.  
  81.         return lst.size();
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement