-6

Given a string S consisting only of lowercase letters check if the string has all characters appearing even times

input : abaccaba

Output : Yes

Explanation: ‘a’ occurs four times, ‘b’ occurs twice, ‘c’ occurs twice and the other letters occur zero times.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
ALOK RAI
  • 1
  • 1
  • Create a map of character occurences from your string and iterate over it to check. – Michał Krzywański May 08 '19 at 07:04
  • 2
    Ok, we know what your homework is, what now? – Amongalen May 08 '19 at 07:05
  • 1
    That duplicate question tells you how to compute the frequency of letters. But please: turn to the [help] to learn how/what to ask here. It is really not appreciated to dump your homework assignment here, assuming that other people provide free tutor services and work with you through **your** homework. – GhostCat May 08 '19 at 07:10
  • You could use a bitmask – Lino May 08 '19 at 07:19

1 Answers1

-1
    String s = "abaccaba";
    Map<Character, Integer> map = new HashMap<Character, Integer>();

    for(int i=0;i<s.length();i++){
        if(!map.containsKey(s.charAt(i))){
            map.put(s.charAt(i), 1);
        }
        else {
            int num = map.get(s.charAt(i));
            map.put(s.charAt(i), ++num);
        }
    }
    for(int i=0;i<s.length();i++){

        int value = map.get(s.charAt(i));
        if(value%2!=0) System.out.println(s.charAt(i) +" is repeated odd number of times");
    }
Nagarjuna
  • 33
  • 2
  • 7