- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
public class Chapter4 {
/*
* find minimal difference symbols words in line; if words count > 0, print
* first word;
*/
public LinkedList<String> wordsList = new LinkedList<>();
public ArrayList<String> minUniqueSimbolWords = new ArrayList<String>();
final int wordsCount = 3;
public void run() {
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();
addMinUniqueSimbolCountWordsToList();
if (minUniqueSimbolWords.isEmpty()) {
System.out.println("not unique words");
return;
}
printUniqueSimbolWords();
}
private void printUniqueSimbolWords() {
System.out
.println("");
for (String s : minUniqueSimbolWords) {
System.out.println(s);
}
}
private void addMinUniqueSimbolCountWordsToList() {
for (String word : wordsList) {
if (minUniqueSimbolWords.isEmpty()) {
minUniqueSimbolWords.add(word);
} else {
int count = getUniqueSimbolCount(word.toCharArray());
addMinUniqueSimbolsCountWord(word, count);
}
}
}
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();
}
} // end class
guest 24.08.2013 20:58 # 0
kegdan 24.08.2013 21:01 # 0
spivti 24.08.2013 21:06 # 0
это тоже работает, принимаются замечания , где что подправить можно. пасибо.
kegdan 24.08.2013 21:25 # 0