4.确定两串乱序同构

题目描述

给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。这里规定大小写为不同字符,且考虑字符串重点空格。

给定一个string stringA和一个string stringB,请返回一个bool,代表两串是否重新排列后可相同。保证两串的长度都小于等于5000。

测试样例:
"This is nowcoder","is This nowcoder"
返回:true
 
"Here you are","Are you here"
返回:false

代码如下:
import java.util.*;

public class Same {
    public boolean checkSam(String stringA, String stringB) {
       int[]table1=new int[256];
       int[]table2=new int[256];
       int len1=stringA.length();
       int len2=stringB.length();
       if(len1!=len2){
           return false;
       }else{
           for(int i=0;i<len1;i++){
               char t=stringA.charAt(i);
               table1[t-'']++;
           }
            for(int i=0;i<len2;i++){
               char t=stringB.charAt(i);
               table2[t-'']++;
           }
       
        for(int i=0;i<256;i++){
            if(table1[i]!=table2[i])
                return false;
        }
       }
        return true;
    }
}

  

原文地址:https://www.cnblogs.com/mlz-2019/p/4750950.html