File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed
leetcode.com 383. Ransom Note Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def canConstruct (self , ransomNote : str , magazine : str ) -> bool :
3
+ left , right = list (ransomNote ), list (magazine )
4
+
5
+ for c in left :
6
+ try :
7
+ idx = right .index (c )
8
+ right .pop (idx )
9
+ except ValueError :
10
+ return False
11
+
12
+ return True
Original file line number Diff line number Diff line change
1
+ from unittest import TestCase
2
+ from main import Solution
3
+
4
+ class TestSolution (TestCase ):
5
+ def test1_can_construct (self ):
6
+ sln = Solution ()
7
+ self .assertEqual (False , sln .canConstruct ('a' , 'b' ))
8
+
9
+ def test2_can_construct (self ):
10
+ sln = Solution ()
11
+ self .assertEqual (False , sln .canConstruct ('aa' , 'ab' ))
12
+
13
+ def test3_can_construct (self ):
14
+ sln = Solution ()
15
+ self .assertEqual (True , sln .canConstruct ('aa' , 'aab' ))
You can’t perform that action at this time.
0 commit comments