http://www.technicalpage.net/search/label/SQL

Repeatedword

 Find repeated word with different or same spelling in a sentence. Such as in "this is the file about the life of wild animals". In this sentence the 'file' and 'life' has same spelling. So the output = life

package abc;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

 

public class FindRepeatedWordwithSameSpelling {

        static Map<String, String> mp = new HashMap<>();

      

             public static String methodToSortWord(String str) {

//           String word = "nedarland";

             String word = str;

             int wordLength = word.length();

             String[] arr = new String[wordLength];

             for(int i=0;i<wordLength;i++) {

                    arr[i]=Character.toString(word.charAt(i));                 

             }

             Arrays.sort(arr); //SORTING ARRAY

             System.out.println("sorted array ==== "+Arrays.toString(arr));//[a, a, d, d, e, l, n, n, r] for nedarland

            

             String sortedWord = " ";//This does not work other way like "" or null

             for(int i=0;i<wordLength;i++) {

                    sortedWord = sortedWord.concat(arr[i]).trim();

             }

             System.out.println("sortedWord ==== "+sortedWord);//aaddelnnr for nedarland

             return sortedWord;

       }

 

       public static void main(String[] args) {

        Scanner scanString = new Scanner(System.in);

        System.out.println("Enter the Strings");

             String givenString = scanString.nextLine();

             String[] splitedWords = givenString.split(" ");

 

             ArrayList<String> alist = new ArrayList<>();

             for(String words:splitedWords) {

                    System.out.println(words);

                    alist.add(words);

             }

             System.out.println("Array of Split words : "+alist);//[this, is, the, file, about, the, life, of, wild, animals]

            

             ArrayList<String> alistSortedWords = new ArrayList<>();

             for(int i=0;i<alist.size();i++) {

                    String sortedWord = FindRepeatedWordwithSameSpelling.methodToSortWord(alist.get(i));

                    alistSortedWords.add(sortedWord);                   

             }

             System.out.println("Array of Split sorted words : "+alistSortedWords);//[hist, is, eht, efil, abotu, eht, efil, fo, dilw, aailmns]

 

//Adding in hashmap

             for(int i=0;i<alist.size();i++) {

                    String key = alistSortedWords.get(i);

                    String value = alist.get(i);

                    mp.put(key,value);

             }

            

//Finding the Repeated word

             String expectedSortedWord = null;

             for(int i=0;i<alistSortedWords.size();i++) {

                    for(int j=0;j<alistSortedWords.size();j++) {

//                         while(!(i==j)) {//this makes infinite loop

                           if(!(i==j)) {

//                               System.out.println("i and j : "+i+" & "+j);

                                  if(alistSortedWords.get(i).equals(alistSortedWords.get(j))) {

                                        expectedSortedWord = alistSortedWords.get(i);

                                 }

                           }

                    }

             }

             System.out.println("The repeated SORTED word : "+expectedSortedWord);//efil

             System.out.println("The REPEATED word : "+mp.get(expectedSortedWord));//life

            

       }

      

      

Output:

Enter the Strings

this is the file about the life of wild animals

this

is

the

file

about

the

life

of

wild

animals

Array of Split words : [this, is, the, file, about, the, life, of, wild, animals]

sorted array ==== [h, i, s, t]

sortedWord ==== hist

sorted array ==== [i, s]

sortedWord ==== is

sorted array ==== [e, h, t]

sortedWord ==== eht

sorted array ==== [e, f, i, l]

sortedWord ==== efil

sorted array ==== [a, b, o, t, u]

sortedWord ==== abotu

sorted array ==== [e, h, t]

sortedWord ==== eht

sorted array ==== [e, f, i, l]

sortedWord ==== efil

sorted array ==== [f, o]

sortedWord ==== fo

sorted array ==== [d, i, l, w]

sortedWord ==== dilw

sorted array ==== [a, a, i, l, m, n, s]

sortedWord ==== aailmns

Array of Split sorted words : [hist, is, eht, efil, abotu, eht, efil, fo, dilw, aailmns]

The repeated SORTED word : efil

The REPEATED word : life

No comments:

Post a Comment