Guest User

Untitled

a guest
Mar 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int minStickers(vector<string>& stickers, string target) {
  4. int n = target.size(), len = stickers.size();
  5. //dp represents minimum sticker required for current state
  6. //state is a binary number where each bit represents if corresponding char appears
  7. vector<int> dp(1 << n, -1);
  8. dp[0] = 0;
  9. for(int i = 0; i < 1 << n; ++i)
  10. {
  11. if(dp[i] == -1)continue;
  12. for(auto sticker : stickers)
  13. {
  14. int state = i;
  15. for(int j = 0; j < sticker.size(); ++j)
  16. {
  17. for(int k = 0; k < n; ++k)
  18. {
  19. //if we have two same chars, two 'a's
  20. //if the first 'a' is matched, we don't
  21. //want the second 'a' to match the same
  22. //position in i again
  23. if((state >> k & 1) == 1)continue;
  24. if(sticker[j] == target[k])
  25. {
  26. //if current char c in sticker is matched
  27. //it shouldn't match another char in i again,
  28. //we just break
  29. state |= 1 << k;
  30. break;
  31. }
  32. }
  33. }
  34. if(dp[state] == -1 || dp[i] + 1 < dp[state])
  35. dp[state] = dp[i] + 1;
  36. }
  37. }
  38. return dp[(1 << n) - 1];
  39. }
  40. };
Add Comment
Please, Sign In to add comment