Skip to content

Instantly share code, notes, and snippets.

@jmtame
Last active August 29, 2015 14:21

Revisions

  1. jmtame revised this gist May 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tricks.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Very fast way to determine if a string contains all unique characters
    # Space O(1), Time O(N)
    # Assumes ASCII strings. Unicode would use this: chars = Array.new(1,111,998)
    # Assumes ASCII strings. Unicode would require space of Array.new(1,111,998).
    def is_unique(str)
    return false if str.length > 256
    chars = Array.new(256)
  2. jmtame created this gist May 16, 2015.
    12 changes: 12 additions & 0 deletions tricks.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # Very fast way to determine if a string contains all unique characters
    # Space O(1), Time O(N)
    # Assumes ASCII strings. Unicode would use this: chars = Array.new(1,111,998)
    def is_unique(str)
    return false if str.length > 256
    chars = Array.new(256)
    str.each_byte.lazy.each do |c|
    return false if chars[c]
    chars[c] = true
    end
    true
    end