Skip to main content
added 195 characters in body
Source Link
nullbyte
  • 415
  • 3
  • 11

Here's a better solution using a hash setmap:

   public static String encode(String input) {
      final Set<Character>StringBuilder setbuilder = new HashSet<>StringBuilder();
      final StringBuilderMap<Character, builderInteger> indexMap = new StringBuilderHashMap<>();
      for (charint chindex := 0; index < input.toCharArraylength(); index++) {
        char set.add(ch = Character.toLowerCase(chinput.charAt(index));
      for (char ch : inputif(indexMap.toCharArraycontainsKey(ch)) {
         ch   int previous = CharacterindexMap.toLowerCaseget(ch);
            builder.replace(previous, previous+1, ")");
            builder.append(set')');
        }else {
            indexMap.containsput(ch, index);
 ? ')' :         builder.append('(');
        }
    }
    return builder.toString();
    }

It requires O(n) time and O(n) additional space, but it's way faster than yours. Also, try using a StringBuilder if you append strings in a loop.

Here's a better solution using a hash set:

   public static String encode(String input) {
      final Set<Character> set = new HashSet<>();
      final StringBuilder builder = new StringBuilder();
      for (char ch : input.toCharArray())
         set.add(Character.toLowerCase(ch));
      for (char ch : input.toCharArray()) {
         ch = Character.toLowerCase(ch);
         builder.append(set.contains(ch) ? ')' : '(');
      }
      return builder.toString();
    }

It requires O(n) time and O(n) additional space, but it's way faster than yours. Also, try using a StringBuilder if you append strings in a loop.

Here's a better solution using a hash map:

 public static String encode(String input) {
    final StringBuilder builder = new StringBuilder();
    final Map<Character, Integer> indexMap = new HashMap<>();
    for (int index = 0; index < input.length(); index++) {
        char ch = Character.toLowerCase(input.charAt(index));
        if(indexMap.containsKey(ch)) {
            int previous = indexMap.get(ch);
            builder.replace(previous, previous+1, ")");
            builder.append(')');
        }else {
            indexMap.put(ch, index);
            builder.append('(');
        }
    }
    return builder.toString();
}

It requires O(n) time and O(n) additional space, but it's way faster than yours. Also, try using a StringBuilder if you append strings in a loop.

added 68 characters in body
Source Link
nullbyte
  • 415
  • 3
  • 11

Here's a better solution using a hash set:

   public static String encode(String input) {
      final Set<Character> set = new HashSet<>();
      final StringBuilder builder = new StringBuilder();
      for (char ch : input.toCharArray())
         set.add(Character.toLowerCase(ch));
      for (char ch : input.toCharArray()) {
         ch = Character.toLowerCase(ch);
         builder.append(set.contains(ch) ? ')' : '(');
        set.add(ch);}
    }
    return builder.toString();
    }

It requires O(n) time and O(n) additional space, but it's way faster than yours. Also, try using a StringBuilder if you append strings in a loop.

Here's a better solution using a hash set:

   public static String encode(String input) {
    final Set<Character> set = new HashSet<>();
    final StringBuilder builder = new StringBuilder();
    for(char ch : input.toCharArray()) {
        ch = Character.toLowerCase(ch);
        builder.append(set.contains(ch) ? ')' : '(');
        set.add(ch);
    }
    return builder.toString();
}

It requires O(n) time and O(n) additional space, but it's way faster than yours. Also, try using a StringBuilder if you append strings in a loop.

Here's a better solution using a hash set:

   public static String encode(String input) {
      final Set<Character> set = new HashSet<>();
      final StringBuilder builder = new StringBuilder();
      for (char ch : input.toCharArray())
         set.add(Character.toLowerCase(ch));
      for (char ch : input.toCharArray()) {
         ch = Character.toLowerCase(ch);
         builder.append(set.contains(ch) ? ')' : '(');
      }
      return builder.toString();
    }

It requires O(n) time and O(n) additional space, but it's way faster than yours. Also, try using a StringBuilder if you append strings in a loop.

Source Link
nullbyte
  • 415
  • 3
  • 11

Here's a better solution using a hash set:

   public static String encode(String input) {
    final Set<Character> set = new HashSet<>();
    final StringBuilder builder = new StringBuilder();
    for(char ch : input.toCharArray()) {
        ch = Character.toLowerCase(ch);
        builder.append(set.contains(ch) ? ')' : '(');
        set.add(ch);
    }
    return builder.toString();
}

It requires O(n) time and O(n) additional space, but it's way faster than yours. Also, try using a StringBuilder if you append strings in a loop.