? Given? An? arbitrary? Ransom? Note? String another? containing? Letters from? All magazines? Write a? function? That would return? True if? Ransom? Note: Can constructed? from the magazines; otherwise,? It--it'll return false. ??
Each letter? In? The? Magazine? string? can? Only is? Used? Once? In? Your? Ransom? Note.
Note:
You may assume this both strings contain only lowercase letters.
Canconstruct ("A", "B")-Falsecanconstruct ("AA", "AB"), falsecanconstruct ("AA", "AaB"), True
The code is as follows:
1 Public classSolution {2 Public Booleancanconstruct (String ransomnote, String magazine) {3 if(Ransomnote.length () <=magazine.length ())4 {5 intI=0;6 while(i<ransomnote.length ())7 {8 intindex=Magazine.indexof (Ransomnote.charat (i));9 if(Index!=-1)Ten { OneMagazine= Magazine.replacefirst (magazine.substring (index,index+1), ""); Ai++; - } - Else return false; the } - } - Else return false; - + return true; - } +}
383. Ransom Note