1

I want to find occurrence of each character in their increasing order.

For eg. for input abcddec output should be a1b1c1d1d2e1c2 , but my code is giving me output as a1b1c2d2e1c2

what changes I should make?

package com.Java8;

public class Occurences {

    public static void main(String[] args) {

        String str = "abb";
        char[] arr = str.toCharArray();
        String result = "";
        for (int i = 0; i < arr.length; i++) {
            int count = 0;
            for (int j = 0; j < arr.length; j++) {
                if (arr[i] == arr[j]) {
                    count++;
                }
            }
            result = result + arr[i] + count;
        }
        System.out.println(result);
    }
}
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
Jav
  • 115
  • 1
  • 4
  • 15
  • You don't need to iterate over the string that much, you need only one `for` loop actually. You just need to store the occurrences of the characters you encountered. – Julien Lopez Jul 07 '16 at 08:35
  • take a look here as well - http://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string – sash Jul 07 '16 at 08:36

5 Answers5

1
String str = "abcddecca";
char[] arr = str.toCharArray();
StringBuilder sb = new StringBuilder();
Map<Character, Integer> counters = new HashMap<>();
for( int i = 0; i < arr.length; i++ ) {
  Integer count = counters.get( arr[i] );
  if ( count == null ) {
    count = 1;
  } else {
    count++;
  }
  counters.put( arr[i], count );
  sb.append( arr[i] );
  sb.append( count );
}
System.out.println( sb );

I would prefer to create some counters state holder and avoid double FOR loop. Also it's not a good practice to use String concatenation in loops,it's better to use StringBuilder.

Ivan Lymar
  • 2,180
  • 11
  • 26
1

Avoid the double for. Store the state in a map or something:

    String str="abbcece";
    char []charArray=str.toCharArray();
    StringBuilder result = new StringBuilder();
    Map<Character, Integer> occurenceMap = new HashMap<Character, Integer>();
    for(Character character:charArray){
        Integer occ = 1;
        if(occurenceMap.containsKey(character)){
            occ =  occurenceMap.get(character)+1;
        }
        occurenceMap.put(character, occ);
        result.append(character).append(occ);
    }

    System.out.println(result.toString());
Ishan Soni
  • 273
  • 3
  • 13
0

Your counting technique counts every time the character appears in the string, not every time the character appears before it in the string + 1. You need to change the forloop to something like this:

for (int i = 0; i < arr.length; i ++){
    int count = 1;
    for (int j = 0; j < i; j ++){
        if(arr[i] == arr[j]){
            count ++;           
        }
    }
    result = result + arr[i] + count;
}

This will iterate through every character before the character in the forloop and check if they are equal

masteryoom
  • 81
  • 6
0

Your inner for loop upper limit should be less than 'i'.

public class Occurences {
    public static void main(String[] args) {
        String str = "abb";
        char[] arr = str.toCharArray();
        String result = "";
        for (int i = 0; i < arr.length; i++) {
            int count = 1;
            for (int j = 0; j < i; j++) { //j upper limit should be i
                if (arr[i] == arr[j]) {
                    count++;
                }
            }
            result = result + arr[i] + count;
        }
        System.out.println(result);
    }
}
CrazyJavaLearner
  • 368
  • 8
  • 17
0

You can use a Map to count the occurrence of each letters:

public static void main(String[] args){
    String s = "abcddec"; 
    Map<Character, Integer> mapCount = new HashMap<>();
    StringBuilder sb = new StringBuilder();

    for (char c : s.toCharArray()){
        if(mapCount.containsKey(c)){
            mapCount.put(c, mapCount.get(c) +1);
        }
        else mapCount.put(c, 1);
        sb.append(String.valueOf(c) + mapCount.get(c));
    }
    System.out.println(sb.toString());
}

Time complexity of the solution is O(N)

L01c
  • 1,033
  • 10
  • 19