We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Strings
  4. String Construction
  5. Discussions

String Construction

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 846 Discussions, By:

votes

Please Login in order to post a comment

  • raffiismail
    6 years ago+ 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.

    93|
    Permalink
  • i_am_malav
    6 years ago+ 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());
        }
    }
    

    }

    33|
    Permalink
  • jpritcha3_14
    5 years ago+ 0 comments

    Just the length of the set of characters in the given string. Only need to add one line in python.

    30|
    Permalink
  • SpencerMKSmith
    6 years ago+ 0 comments

    @WatchandLearn

    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.

    27|
    Permalink
  • alpeshksuthar
    5 years ago+ 0 comments

    In Python:

    for i in range( int( input() ) ):
        print(len(set(input())))
    
    20|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature