Using the data structures (Hashmap) I was able to do it.
This code is:
import java.util. *; Square unique {public static zero main (string [] args) {Hashmap < Character, integer & gt; CharMap = New Hashmop & lt; Character, integer & gt; (); Boolean is not = unknown = false; (Four loops: args [0]. Kocharare ()) {integer freq = charMap.get (loop); CharMap.put (Loop, (freq == zero)? 1: freq + 1); If (charMap.get (loop)> 1) {isNotUnique = true; }} System.out.println (isNotUnique); }} Without datastro, I came up with a blunt approach. It is O (n ^ 2)
class unique {public static zero main (string [] args) {string input string = args [0]; System.out.println (isUnique (inputstring)); } Private Stable Boolean is Unic (String InputString) {String Method String = Input String; (Int i = 0; i & lt; inputString.length (); i ++) {for (int j = i + 1; j & lt; inputstring.length (); j ++) {if (methodString .charAt (i) == MethodString.charAt (j)) {return false; }}} Be true; }} I was thinking that it is possible to solve the complexity of time (n)
If you need to support Unicode characters that are not represented by four pairs of rentals, this will:
Personal Stabilize Boolean Unic (String inputstring) {long [] used = new tall [1024]; (Four C: InputString.Cashare ()) {If (([>>> <6> and (1 )> 0) {return false; } [C & gt; & Gt; & Gt; 6]. = 1 & lt; & Lt; C; } Back true; } This is using bit flip to save memory. This is essentially the same thing as if you used an array of boolean: < Pre> Personal Stable Boolean is Unique 2 (String Input String) {Boolean [] used = New Boolean [65536]; (Four C: Input String, Charaire ()) {if (used [c]) {return false; } [C] = true; } Back true; }
If you only need to support ASCII characters, you can limit the size of used in any case to reduce the memory ( Therefore long [4]] and boolean [256] ). Under a certain length of inputString it is possible to allocate memory for it to check n ^ 2, however, ideally however you combine two based on length.
If you need to support all possible Unicode characters, then you have to modify it to support the surrogate four pairs. You can find them with the character. Highest (C) . Look for some help and search Google for more details.
Comments
Post a Comment