Thursday 24 November 2016

Printing characters with their count in a String

Total number of Character in ASCII table is 256 (0 to 255). 0 to 31(total 32 character ) is called as ASCII control characters (character code 0-31). 32 to 127 character is called as ASCII printable characters (character code 32-127). 128 to 255 is called as The extended ASCII codes (character code 128-255)

There are multiple solution available with O(n) time and space complexity using Set,Map , O(n2) using Brute force iterating each character element of a String.

Solution 1 : 

But I am representing a solution with O(n) time complexity and In place O(1) space complexity as follow : 



/**

 * @author sachin
 *
 */
public class CountDuplicates {
public static void charsCount(String str){
int[] intArr = new int[256];
char[] charArrInputstr.toCharArray();
int charLength = charArrInput.length;
for (int i = 0; i < charLength; i++) {
intArr[charArrInput[i]]++;
}
int j =0;
for (int c : intArr) {
if(c > 0){
System.out.println( "char=" + (char)j + " and count=" + c );
}
j++;
}
}
public static void main(String[] args) {
String a = "My Name Is Sachin Srivastava and I am a Java Architect passionate "
+ "about writing,blogging & coding , author of tek9g.blogspot.com and vedology.blogspot.com";
charsCount(a);
}
}

Output :


char=  and count=21
char=& and count=1
char=, and count=2
char=. and count=4
char=9 and count=1
char=A and count=1
char=I and count=2
char=J and count=1
char=M and count=1
char=N and count=1
char=S and count=2
char=a and count=15
char=b and count=4
char=c and count=6
char=d and count=4
char=e and count=5
char=f and count=1
char=g and count=9
char=h and count=3
char=i and count=8
char=k and count=1
char=l and count=4
char=m and count=4
char=n and count=7
char=o and count=14
char=p and count=3
char=r and count=4
char=s and count=6
char=t and count=10
char=u and count=2
char=v and count=4
char=w and count=1
char=y and count=2


Solution 2 :

Using a HashMap : O(n) time and space complexity


       
Map<Character, Integer> map = new HashMap<>();
char[] charArrInputstr.toCharArray();
for (char c : charArrInput) {
if(!map.containsKey(c)){
map.put(c, 1);
} else {
map.put(c, map.get(c)+1);
}
}
for (Entry<Character, Integer> entry : map.entrySet()) {
System.out.println("char=" + entry.getKey() + " and count=" + entry.getValue());
}
Output:
char=A and count=1
char=I and count=2
char=J and count=1
char=M and count=1
char=N and count=1
char=S and count=2
char=  and count=21
char=a and count=15
char=b and count=4
char=c and count=6
char=d and count=4
char=e and count=5
char=& and count=1
char=f and count=1
char=g and count=9
char=h and count=3
char=i and count=8
char=k and count=1
char=, and count=2
char=l and count=4
char=m and count=4
char=n and count=7
char=. and count=4
char=o and count=14
char=p and count=3
char=r and count=4
char=s and count=6
char=t and count=10
char=u and count=2
char=v and count=4
char=w and count=1
char=y and count=2
char=9 and count=1


Solution 3 :   Using brute force in the O(n2)   , basically iterating each  n element and comparing and counting remaining  n-1 element  making it quadratic solution with respect to time.

Solution 3 is not recommended , Solution 1 is the best solution and if one not able to do like solution1 then Solution 2 would be simple to use.

Enjoy tek9g  blog and coding !!!!!!!!!

No comments:

Post a Comment