比较字符串

来源:互联网 发布:大淘客cms加淘口令 编辑:程序博客网 时间:2024/05/22 06:49
比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母
 注意事项
在 A 中出现的 B 字符串里的字符不需要连续或者有序。
样例
给出 A = "ABCD" B = "ACD",返回 true

给出 A = "ABCD" B = "AABC", 返回 false


import java.util.Scanner;/** * 比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母 注意事项在 A 中出现的 B 字符串里的字符不需要连续或者有序。样例给出 A = "ABCD" B = "ACD",返回 true给出 A = "ABCD" B = "AABC", 返回 false *  * @author Dell * */public class Test55 {   public static boolean compareStrings(String A, String B){     if(B.equals(" "))     return true;    boolean[] flag=new boolean[A.length()];    int count=0;     for(int i=0;i<B.length();i++)     {     for(int j=0;j<A.length();j++)     {     if(A.charAt(j)==B.charAt(i)&&flag[j]==false)     {      flag[j]=true;     count++;     break;     }          }     }     if(count==B.length())     return true;     else            return false;  }public static void main(String[] args) {    Scanner sc=new Scanner(System.in);    String A=sc.nextLine();    String B=sc.nextLine();    System.out.println(compareStrings(A,B));}}


原创粉丝点击