Compare Strings
Compare two strings A and B, determine whether A contains all of the characters in B.
The characters in string A and B are all Upper Case letters.
Example
For A = "ABCD"
, B = "ACD"
, return true
.
For A = "ABCD"
, B = "AABC"
, return false
.
Note
The characters of B in A are not necessary continuous or ordered.
public class Solution { /** * @param A : A string includes Upper Case letters * @param B : A string includes Upper Case letter * @return : if string A contains all of the characters in B return true else return false */ public boolean compareStrings(String A, String B) { // write your code here int m=A.length(); int n=B.length(); if(n>m) return false; boolean[] isCount=new boolean[m]; int count=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(B.charAt(i)==A.charAt(j) && isCount[j]==false) { count++; isCount[j]=true; break; } } } if(count==n) return true; else return false; } }
时间: 2024-10-24 22:36:39