how to count the number of words of the same(PALINDROME) in java

北城余情 提交于 2019-12-02 11:14:16

Read the file into your program, split the entries at every space and enter those into an arraylist. Afterwards, apply your palindrome algorithm onto each value in your arraylist and keep track of the words that were a palindrome and their occurences (for example a 2D array, or an arraylist with an object that holds both values).

When you've followed these steps, you should pretty much be there. More specific help will probably be given once you've shown attempts of your own.

Using Collections in java will reduce the programming effort

Algorithm :

  1. Read the paragraph to a String variable

  2. Split the String using StringTokenizer using token as ' '(space) and add each word to ArrayList (Set wont allow duplicates)

  3. Write a method which return boolean (TRUE/ FALSE) value based on whether a given String is palindrome or not.

  4. Define a Map to hold the values of palindrome String and number of times it is repeated.

  5. If yes add the String to Map with key as palindrome String and value as number of times else dont add the String to Map

  6. Repeat the same logic until all the words are finished

Sample Code:

` public class StringPalindromeCalculator {

   private Map<String, int> wordsMap = new HashMap<>();
   private List<String> wordsList = new ArrayLiat<>();

   private boolean isPalindrome(String inputString) {
      // write String palindrome logic here
   }

   public Map<String, int> findPalindromeWords(String completeString) {
       StringTokenizer wordTokenizer = new StringTokenizer(completeString, ' ');
       while(wordTokenizer.hasMoreTokens()) {
            wordsList.add(wordTokenizer.nextToken());
       }
       for(String word : wordsList) {
            if(isPalindrome(word)) {
                 if(wordsMap.containsKey(word)) {
                      // increment the value of word
                 }
            } else {
                    // put the word into Map and return the map value 
            }

       }
       return wordsMap;
   }  

}`

Hope this Helps :)

public class Palindrome {

int count = 0;

public static void main(String[] args) {
    String a = "malayalammadyoydaraarasdasdkfjasdsjhtj";
    Palindrome palindrome = new Palindrome();
    palindrome.countPalin(a);
}

private int countPalin(String str) {
    for (int i = 0; i < str.length() - 1; i++) {
        char start = str.charAt(i);
        String st = "";
        st += start;
        for (int j = i + 1; j < str.length(); j++) {

            st += str.charAt(j);

            StringBuffer rev = new StringBuffer(st).reverse();
            if (st.equals(rev.toString()) && st.length() > 1) {
                System.out.println(st.toString());
                count++;
            }
        }

        st = "";
    }

    System.out.println("Total Count : " + count);

    return count;
}

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!