给定string stringA和string stringB,编写程序确认两字符串包含的字符是否完全相同,注意大小写为不同字符,且考虑字符串中的空格,返回一个bool,代表两串是否由一样的字符组成。保证两串的长度都小于等于5000。
给定string stringA和string stringB,编写程序确认两字符串包含的字符是否完全相同,注意大小写为不同字符,且考虑字符串中的空格,返回一个bool,代表两串是否由一样的字符组成。保证两串的长度都小于等于5000。
"This is nowcoder","is This nowcoder"
true
"Here you are","Are you here"
false
/*同理,字符按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; } }