#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
# ID Name

__author__ = 'Libao Jin'
__date__ = '02/11/2017'

import time

class Solution(object):
    def findLongestWord(self, s, d):
        """
        :type s: str
        :type d: List[str]
        :rtype: str
        """
        ls = list(s)
        cur_max = 0
        cur_word = ''
        for w in d:
            ls_temp = ls
            lw = list(w)
            n = len(ls_temp)
            m = len(lw)
            for l in lw:
                if l not in ls_temp:
                    continue
                else:
                    ls_temp.remove(l)

            if (len(ls_temp)) == n-m and cur_max < m:
                cur_max = m
                cur_word = w
                print(cur_max, cur_word)
        return cur_word

    def test(self):
        start_time = time.time()
        s = "abpcplea"
        d = ["ale", "apple", "monkey", "plea"]
        result = self.findLongestWord(s, d)
        end_time = time.time()
        print(result)
        print('Elapsed time: {0:.6f}'.format(end_time - start_time))

if __name__ == '__main__':
    s = Solution()
    s.test()
