String Subset

http://programmingpraxis.com/2010/11/23/string-subsets/

import operator
import collections        

def is_str_subset(sub, base):
    D = collections.defaultdict(lambda : 0)
    for c in list(base):
        D[c] += 1
    for c in list(sub):
        D[c] -= 1
        if D[c] < 0:
            return False
    return True


if __name__ == '__main__':
    str1 = "DA"
    str2 = "ABCD"
    str3 = "DAD"
    print "Is str1 a string subset of str2?\t%s" % is_str_subset(str1, str2)
    print "Is str3 a string subset of str2?\t%s" % is_str_subset(str3, str2)

scheme

(use gauche.experimental.lamb)
(use srfi-13)

(define (str-subset? sub base)
  (let1 ht (make-hash-table 'eqv?)
    (string-for-each (^c (hash-table-update! ht c (cut + <> 1) 0)) 
                     base)
    (let/cc break
      (string-for-each (^c (let1 n (hash-table-get ht c 0)
                             (cond ((<= n 0) (break #f))
                                   (else (hash-table-update! ht c (cut - <> 1) 0)))))
                       sub)
      #t)))

(str-subset? "abd" "abcd") ; => #t
(str-subset? "dad" "abcd") ; => #f