Posts

Showing posts with the label count distinct characters

Java: Given a String find the distinct characters in it and count it

Image
  Given a String find the distinct characters in it and count it.   Input:   DISTINCT Output: DSNC 4 Approach: ·          Well, to approach this problem there are ‘N’ number of ways. ·          Convert string to character array. ·          Use HashMap to hold keys as characters and count as values. ·          Use any Set implemented Class add the characters and get the count. ( we can get distinct but need to remove repeated character ) ·          Use traditional Brute force method with multiple for loops, check each character and get the count. ·          Create your custom class with member variables as character and count add in to an ArrayList and get the count. ·        ...