Blue Ocean: check if one string has anagram substring of the other string
Most of the anagram related question can be solved by using int array, each element in the array representing the number of appearances of each char in the string.
* Given 2 strings, check if any one of them
* has any anagram of the other string, as a substring of it.
*
* here without losing generality s1.length() > s2.length()
*
* time complexity: O(L2) + O(L1)*O(1) = O(L1)
*/
public static boolean hasAnagramSubstring(String s1, String s2){
assert s1.length() > s2.length();
final int ALPHA_SIZE = 256;
int[] store2 = new int[ALPHA_SIZE];
for(char c : s2.toCharArray())
store2[c] ++;
char[] chars = s1.toCharArray();
int[] store1 = new int[ALPHA_SIZE];
for(int i=0; i<s2.length(); i++){
store1[chars[i]]++;
}
//count the number of different characters in each string
int numOfNonZero = 0;
for(int i=0; i<ALPHA_SIZE; i++){
//store the difference between two int array in store1
store1[i] = store1[i] - store2[i];
if(store1[i]!=0)
numOfNonZero++;
}
if(numOfNonZero==0)
return true;
for(int i = s2.length(); i<s1.length(); i++){
store1[chars[i]]++;
if(store1[chars[i]]==0)
numOfNonZero--;
store1[chars[i-1]]--;
if(store1[chars[i-1]]==0)
numOfNonZero--;
if(numOfNonZero==0)
return true;
}
return false;
}
Related: Anagram Substring Search (Or Search for all permutations)
http://www.geeksforgeeks.org/check-whether-two-strings-are-anagram-of-each-other/
Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other.
Read full article from Blue Ocean: check if one string has anagram substring of the other string
Most of the anagram related question can be solved by using int array, each element in the array representing the number of appearances of each char in the string.
* Given 2 strings, check if any one of them
* has any anagram of the other string, as a substring of it.
*
* here without losing generality s1.length() > s2.length()
*
* time complexity: O(L2) + O(L1)*O(1) = O(L1)
*/
public static boolean hasAnagramSubstring(String s1, String s2){
assert s1.length() > s2.length();
final int ALPHA_SIZE = 256;
int[] store2 = new int[ALPHA_SIZE];
for(char c : s2.toCharArray())
store2[c] ++;
char[] chars = s1.toCharArray();
int[] store1 = new int[ALPHA_SIZE];
for(int i=0; i<s2.length(); i++){
store1[chars[i]]++;
}
//count the number of different characters in each string
int numOfNonZero = 0;
for(int i=0; i<ALPHA_SIZE; i++){
//store the difference between two int array in store1
store1[i] = store1[i] - store2[i];
if(store1[i]!=0)
numOfNonZero++;
}
if(numOfNonZero==0)
return true;
for(int i = s2.length(); i<s1.length(); i++){
store1[chars[i]]++;
if(store1[chars[i]]==0)
numOfNonZero--;
store1[chars[i-1]]--;
if(store1[chars[i-1]]==0)
numOfNonZero--;
if(numOfNonZero==0)
return true;
}
return false;
}
Related: Anagram Substring Search (Or Search for all permutations)
http://www.geeksforgeeks.org/check-whether-two-strings-are-anagram-of-each-other/
Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other.
bool areAnagram(char *str1, char *str2){ // Create 2 count arrays and initialize all values as 0 int count1[NO_OF_CHARS] = {0}; int count2[NO_OF_CHARS] = {0}; int i; // For each character in input strings, increment count in // the corresponding count array for (i = 0; str1[i] && str2[i]; i++) { count1[str1[i]]++; count2[str2[i]]++; } // If both strings are of different length. Removing this // condition will make the program fail for strings like // "aaca" and "aca" if (str1[i] || str2[i]) return false; // Compare count arrays for (i = 0; i < NO_OF_CHARS; i++) if (count1[i] != count2[i]) return false; return true;}bool areAnagram(char *str1, char *str2){ // Get lenghts of both strings int n1 = strlen(str1); int n2 = strlen(str2); // If length of both strings is not same, then they // cannot be anagram if (n1 != n2) return false; // Sort both strings quickSort(str1, 0, n1 - 1); quickSort(str2, 0, n2 - 1); // Compare sorted strings for (int i = 0; i < n1; i++) if (str1[i] != str2[i]) return false; return true;}