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

CharacterCount

Count character in a word and display number and character together  

// Find number of characters with number in given word.

// if you are given a word = aaabbbddddmmmmmsszzz then return [3a, 3b, 4d, 5m, 2s, 3z] 

package abc;

import java.util.ArrayList;

public class CharacterCount{

       public static void main(String[] Args) {

              CharacterCount.findCharCount();

       }

      

       public static void findCharCount() {

            

             String word = "aaabbbddddmmmmmsszzz";

             int wordLength = word.length();

            

             //Find the indexes where the change of char occuring

             ArrayList<Integer> alist1 = new ArrayList<>();

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

                    if(!((Character.toString(word.charAt(i-1)).equals(Character.toString(word.charAt(i)))))){

                           alist1.add(i-1);

                    }

                    if(i==wordLength-1){

                           alist1.add(wordLength-1);//if you use alist1.add(i), then here it would be alist1.add(wordLength)

                    }

             }

             System.out.println("alist1 = "+alist1);//[2, 5, 9, 14, 16, 19]

            

            

             //find the last char at which the change is occurring

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

             int alist1Length = alist1.size();

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

                    String charAtAlist1Indexes = Character.toString(word.charAt(alist1.get(i)));//if you use alist1.add(i) above, then here it would be Character.toString(word.charAt(alist1.get(i-1)))

                           alist2.add(charAtAlist1Indexes);

             }

             System.out.println("alist2 = "+alist2);//[a, b, d, m, s, z]

 

 

             //To get exact number of each char

             ArrayList<Integer> alist3 = new ArrayList<>();

             int alist2Length = alist2.size();

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

                    if(i==0) {

                           alist3.add(alist1.get(i) +1); //alist1 first index value plus the zero index, //if you use alist1.add(i) above, then here it would be alist3.add(alist1.get(i))

                    } else {

                           alist3.add((alist1.get(i))-(alist1.get(i-1)));      

                    }

             }

             System.out.println("alist3 = "+alist3);//[3, 3, 4, 5, 2, 3]

            

             //Represent the char with number

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

             int alist3Length = alist3.size();

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

                    String alist4Values = (Integer.toString(alist3.get(i))).concat(alist2.get(i));

                    alist4.add(alist4Values);

             }

             System.out.println("alist4 = "+alist4);//[3a, 3b, 4d, 5m, 2s, 3z] 

       }

}

Output:

alist1 = [2, 5, 9, 14, 16, 19]
alist2 = [a, b, d, m, s, z]
alist3 = [3, 3, 4, 5, 2, 3]
alist4 = [3a, 3b, 4d, 5m, 2s, 3z]


No comments:

Post a Comment