首页 > 试题广场 >

确定两串乱序同构

[编程题]确定两串乱序同构
  • 热度指数:47223 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

给定string stringA和string stringB,编写程序确认两字符串包含的字符是否完全相同,注意大小写为不同字符,且考虑字符串中的空格,返回一个bool,代表两串是否由一样的字符组成。保证两串的长度都小于等于5000。

示例1

输入

"This is nowcoder","is This nowcoder"

输出

true
示例2

输入

"Here you are","Are you here"

输出

false
推荐
Ron头像 Ron
/*同理,字符按ASCII大小在数组中存放对应字符出现个数,若两个字符串中各字符出现字数相同,则返回true,反之false*/
import java.util.*;

public class Same {
    public boolean checkSam(String stringA, String stringB) {
        // write code here
    	int lenA = stringA.length();
    	int lenB = stringB.length();
    	if(lenA != lenB){
    		return false;
    	}
    	int[] strA = new int[256];
    	int[] strB = new int[256];
    	for(int i = 0; i < lenA; i++){
    		strA[stringA.charAt(i)]++;
    		strB[stringB.charAt(i)]++;
    	}
    	for(int i = 0;i<256;i++){
    		if(strA[i]!=strB[i]){
    			return false;
    		}
    	}
    	return true;
    }
}

编辑于 2015-10-04 19:00:13 回复(34)