Java: Given a String find the distinct characters in it and count it
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.
·
Etc.
Thought:
·
What if we are not suppose to use any
additional data structures?
·
When such problems come in – the first thought
will come to our mind is- let’s convert to character Array so that we can
navigate it easily.
·
The focus is on only one method of String class:
toCharArray()
·
But String class have provided many other
methods- even if we know those we fail to identify.
·
When a problem is given to you, split the
problem in to tokens (in your mind) and analyze.
·
Tokens are like:
o String
o Characters
o Distinct
o Count
·
You can get answers in the question many a
times.
·
But the focus and weightage we give to
specific token and revolve around it.
·
Now, can we recall any methods in String class
which are helpful?
Method |
Description |
ReturnType |
indexOf(int
ch) OR indexOf(String str) |
Returns
the index (int value) within this string of the first occurrence of the
specified character/substring. |
int |
lastIndexOf(int
ch) OR lastIndexOf(String str) |
Returns
the index within this string of the last occurrence of the specified
character/substring. |
int |
length() |
Returns
the length of this string. |
int |
charAt(int
index) |
Returns
the char value at the specified index. |
char |
replace(char
oldChar, char newChar) |
Returns
a new string resulting from replacing all occurrences of oldChar in this
string with newChar. |
String |
replace(CharSequence
target, CharSequence replacement) |
Replaces
each substring of this string that matches the literal target sequence with
the specified literal replacement sequence. |
String |
replaceAll(String
regex, String replacement) |
Replaces
each substring of this string that matches the given regular expression with
the given replacement. |
String |
Now knowing the above methods can we try to write code for the problem?
(click on the image to expand)Well, trying to use Java Streams to solve the same problem.
(click on the image to expand)Note:
·
The above
solutions are not the ultimate or the only way to approach the problem.
·
Attaching
screenshot because you need to practice than copying code and executing in IDE.
·
As I said
there are ‘N’ number of ways to solve the issue, here I tried using inbuilt
methods from String class and get the solution.
·
Comments and
suggestions are always welcome. git link:
Comments
Post a Comment