String: determine whether two strings are inflected words

 

Problem Description:

Given two strings, if the types of characters appearing in the two strings are the same and the number of occurrences of each character is the same, then the two strings are inflected.

Assuming that the character codes appearing are between 0-255 (closed interval)

 

Algorithm implementation:

public boolean isDeformation (String str1, String str2) { 

if (str1 == null || str2 == null || str1.length ()! = str2.length ()) {
return false;
}

char [] chars1 = str1. toCharArray ();
char [] chars2 = str2.toCharArray ();
int [] map = new int [256];

for (int i = 0; i <chars1.length; i ++) {
map [chars1 [i]] + +;
}

for (int i = 0; i <chars2.length; i ++) {
if (map [chars2 [i]]-== 0) {// Here is the judgment before performing the decrement operation, when "== "When established, it means that the current elements in chars2 are more
false than chars1 ;
}
}
return true;
}

 

Analysis of Algorithms:

1. First, compare the non-empty judgment and the length of the string;

2. Convert the two strings into character arrays respectively;

3. Add a statistical array to count the values ​​of each character in the first character array;

4. Traverse the second array and compare the values ​​of the characters in the second character array with the statistical values ​​of the first array.

 

Guess you like

Origin www.cnblogs.com/heibingtai/p/12705799.html