public class RemoveDuplicates { static String removeDups(String s) { if(s == null || s.length() <= 1) return s; boolean[] hit = new boolean[256]; int len = s.length(); String res = ""; for( int i = 0 ; i < len ; i++) { if(hit[(int)s.charAt(i)]) { continue; } else { hit[(int)s.charAt(i)] = true; res += s.charAt(i); } } return res; } public static void main(String[] args) { System.out.println(removeDups("asfass")); // Output: -> asf System.out.println(removeDups("aaaasssaa")); // Output: -> as } }Time Complexity: O(n)
Saturday, December 29, 2012
Remove Duplicate Characters in String
Labels:
String
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment