Last active
December 17, 2015 05:49
-
-
Save lifeparticle/5560679 to your computer and use it in GitHub Desktop.
Count the unique character in a String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author: Mahbub | |
// http://mahbubzaman.wordpress.com/2012/06/21/count-the-unique-character-in-a-string/ | |
/* | |
input | |
“aaaaaa” | |
“aaa aaa” | |
“abcdeabcde” | |
“YESyes” | |
output | |
1 | |
2 | |
5 | |
6 | |
*/ | |
public static int frq [] = new int [500]; | |
public static int countUniqueChar (String line) { | |
Arrays.fill(frq, 0); | |
int ans = 0; | |
for(int i = 0; i < line.length(); ++i) { | |
if(frq[line.charAt(i)] == 0) { | |
frq[line.charAt(i)]++; | |
++ans; | |
} | |
} | |
return ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment