Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.HashSet;
- //find a way to replace sorted ArrayLists that are equal with their original order; map doesnt work
- //because assigns both versions to the same original key
- public class SubstringSort{
- class syllsorter implements Comparator<ArrayList<String>>{
- public int compare(ArrayList<String> a, ArrayList<String> b){
- /***
- for(int i=0; i<a.size(); i++){
- int number = a.get(i).compareTo(b.get(i));
- if(number!=0){
- return number;
- }
- }
- if(a.size() > b.size()){
- return 1;
- }
- if(a.size() < b.size()){
- return -1;
- }
- else{
- return 0;
- }
- ***/
- int size = 0;
- if(a.size() <= b.size()){
- size = a.size();
- }
- if(a.size() > b.size()){
- size = b.size();
- }
- for(int i=0; i<size; i++){
- int number = a.get(i).compareTo(b.get(i));
- if(number!=0){
- return number;
- }
- }
- if(a.size() > b.size()){
- return 1;
- }
- if(a.size() < b.size()){
- return -1;
- }
- else{
- return 0;
- }
- }
- }
- public void sort(ArrayList<ArrayList<String>> listoflists){
- Collections.sort(listoflists, new syllsorter());
- }
- public String[] sortSubstrings(String[] wordList) {
- ArrayList<ArrayList<String>> Decomps = new ArrayList<ArrayList<String>>();
- ArrayList<ArrayList<String>> sortedDecomps = new ArrayList<ArrayList<String>>();
- HashMap<ArrayList<String>, ArrayList<String>> map = new HashMap<ArrayList<String>, ArrayList<String>>();
- for(int i=0; i<wordList.length; i++){
- ArrayList<String> decomp = syllseparation(wordList[i]);
- ArrayList<String> decomp2 = syllseparation(wordList[i]);
- Collections.sort(decomp);
- map.put(decomp2, decomp);
- Decomps.add(decomp2);
- sortedDecomps.add(decomp);
- }
- System.out.println(map);
- sort(sortedDecomps);
- System.out.println(sortedDecomps);
- HashSet<Integer> indexes = new HashSet<Integer>();
- for(int i=0; i<sortedDecomps.size()-1; i++){
- for(int j=i+1; j<sortedDecomps.size(); j++){
- if(sortedDecomps.get(i).equals(sortedDecomps.get(j))){
- indexes.add(Decomps.indexOf(Decomps.get(i)));
- indexes.add(Decomps.indexOf(Decomps.get(j)));
- }
- }
- }
- System.out.println(indexes);
- ArrayList<ArrayList<String>> sublist = new ArrayList<ArrayList<String>>();
- for(int i=0; i<sortedDecomps.size(); i++){
- if(indexes.contains(i)){
- for(ArrayList<String> x : map.keySet()){
- if(sortedDecomps.get(i) == map.get(x)){
- sublist.add(x);
- }
- }
- }
- }
- sort(sublist);
- System.out.println(sublist);
- for(int i=0; i<sortedDecomps.size(); i++){
- if(indexes.contains(i)){
- sortedDecomps.set(i, sublist.get(0));
- sublist.remove(0);
- }
- }
- System.out.println(sortedDecomps);
- /***
- sort(sortedDecomps);
- System.out.println(sortedDecomps);
- System.out.println(sublist);
- sort(sublist);
- System.out.println(sublist);
- for(int i=0; i<sublist.size(); i++){
- ArrayList<String> test = sublist.get(i);
- Collections.sort(test);
- for(int j=0; j<sortedDecomps.size(); j++){
- if(test.equals(sortedDecomps.get(j))){
- sortedDecomps.set(sortedDecomps.indexOf(sortedDecomps.get(j)), sublist.get(i));
- break;
- }
- }
- }
- System.out.println(sortedDecomps);
- ***/
- ArrayList<String> temp = new ArrayList<String>();
- for(int i=0; i<sortedDecomps.size(); i++){
- for(ArrayList<String> x : map.keySet()){
- if(sortedDecomps.get(i) == map.get(x)){
- sortedDecomps.set(i, x);
- }
- }
- String str = "";
- for(int j=0; j<sortedDecomps.get(i).size(); j++){
- str += sortedDecomps.get(i).get(j);
- }
- temp.add(str);
- }
- System.out.println(temp);
- String[] answer = new String[temp.size()];
- for(int i=0; i<temp.size(); i++){
- answer[i] = temp.get(i);
- }
- System.out.println(Arrays.toString(answer));
- return answer;
- }
- public ArrayList<String> syllseparation(String word){
- String vowels = "aeiou";
- String consonants = "bcdfghjklmnpqrstvwxyz";
- ArrayList<String> syllables = new ArrayList<String>();
- int count = 0;
- int startIndex = 0;
- for(int i=0; i<word.length(); i++){
- if(i==(word.length()-1)){
- String syll = word.substring(startIndex, word.length());
- syllables.add(syll);
- continue;
- }
- if(vowels.contains(""+word.charAt(i)) && consonants.contains(""+word.charAt(i+1))){
- if(count == 0){
- String syll = word.substring(startIndex, i+1);
- syllables.add(syll);
- startIndex = i+1;
- count++;
- }
- else{
- String syll = word.substring(startIndex, i+1);
- syllables.add(syll);
- startIndex = i+1;
- }
- }
- }
- return syllables;
- }
- public static void main(String[] main){
- SubstringSort a = new SubstringSort();
- String[] wordList = {"ba", "ba", "ba"};
- a.sortSubstrings(wordList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement