- Prepare
- Algorithms
- Strings
- String Construction
- Discussions
String Construction
String Construction
+ 0 comments In PHP:
print count(array_unique(str_split($s))) . "\n";
I think just about everyone figured out that the question meant to simply ask for the number of unique characters in a given string.
+ 0 comments JAVA8 SOLUTION:
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int a0 = 0; a0 < n; a0++){ String s = in.next(); System.out.println(s.chars().distinct().count()); } }
}
+ 0 comments Just the length of the set of characters in the given string. Only need to add one line in python.
+ 0 comments Instead of adding each char to the set and letting the set look through itself to determine if the char has already been added to it (which may be a O(n) operation), you could just do the check yourself. I did it using an array of booleans of size 26. When I see the character, I set the bool value to true (looking up using the letters ASCII value). This is a O(1) operation. This makes the solution O(n) because we only look through each character one time.
+ 0 comments In Python:
for i in range( int( input() ) ): print(len(set(input())))
Sort 846 Discussions, By:
Please Login in order to post a comment