Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.Scanner;
- public class wordsAnalizer {
- public LinkedList<String> wordsList = new LinkedList<>();
- public ArrayList<String> minUniqueSimbolWords = new ArrayList<String>();
- final int wordsCount = 3;
- private void readData() {
- System.out.println("Iveskite " + wordsCount + " zodzius: ");
- Scanner scan = new Scanner(System.in);
- for (int i = 0; i < wordsCount; i++) {
- wordsList.add(scan.nextLine());
- }
- scan.close();
- }
- private void printResults() {
- if (minUniqueSimbolWords.isEmpty()) {
- System.out.println("not unique words");
- return;
- }
- printUniqueSimbolWords();
- }
- public void run() {
- readData();
- selectWordsWithMinimumCountOfUniqueCharacters();
- printResults();
- }
- private void printUniqueSimbolWords() {
- System.out.println("");
- for (String s : minUniqueSimbolWords) {
- System.out.println(s);
- }
- }
- private void processWord(String word) {
- if (minUniqueSimbolWords.isEmpty()) {
- minUniqueSimbolWords.add(word);
- } else {
- int count = getUniqueSimbolCount(word.toCharArray());
- addMinUniqueSimbolsCountWord(word, count);
- }
- }
- private void selectWordsWithMinimumCountOfUniqueCharacters() {
- for (String word : wordsList) {
- processWord(word);
- }
- }
- private void addMinUniqueSimbolsCountWord(String word, int count) {
- int countOfFirstFromList = getUniqueSimbolCount(wordsList.getFirst()
- .toCharArray());
- if (count < countOfFirstFromList) {
- minUniqueSimbolWords.clear();
- minUniqueSimbolWords.add(word);
- } else if (count == countOfFirstFromList) {
- minUniqueSimbolWords.add(word);
- }
- }
- private int getUniqueSimbolCount(char[] str) {
- ArrayList<Character> lst = new ArrayList<Character>();
- for (char c : str) {
- if (!lst.contains(c)) {
- lst.add(c);
- }
- }
- return lst.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement