#crack the code interview handwrite code
#chapter 1 arrays and strings
#1.1 implement an algorithm to determine if a string has all unique characters,
#requirement:no additional data structures
// C++,function
bool is_unique_characters(string str){
bool character[256] = {false};
for(int i=0;i<str.length();i++){
if (character[str[i]] == true)
return false;
else
character[str[i]] = true;
}
return true;
}
//python function
#1.2 write code to reverse a C-style String
#tips: C-style string ends with null character
// C/C++
char* reverse_string(char* str){
int length = 0;
while(*str != NULL)
length++;
int start = 0;
while(start < length){
swap(str[start],str[length]);
start++;
length--;
}
return str;
}
#1.3 To remove the duplicate characters in a string
#requirement: no additional buffer allowed,but one or two additional variables are fine
// C/C++
string remove_duplicate_characters(string str){
bool exist[256];
int duplicate_nums = 0;
int len = str.length();
for(int i = 0;i<len;i++){
if(exist[str[i]] == false)
exist[str[i]] = true;
else{
duplicate_nums++;
for(int j= i;j<len-duplicate_num-2;j++)
str[j] = str[j+1];
}
}
}
#1.4 write an algorithm to decide if two strings are anagrams or not
#tips:the anagrams means having the same nums of the same character
//C/C++
bool is_anagrams(string str1,string str2){
int character_str1[256],character_str2[256];
memset(character_str1,0,sizeof(character_str1));
memset(character_str2,0,sizeof(character_str2));
int length1 = str1.length(),length2 = str2.length();
for(int i=0;i<length1;i++)
character[str1[i]]++;
for(int i=0;i<length2;i++)
character[str2[i]]++;
for(int i=0;i<256;i++)
if(character_str1[i] != character_str2)
return false;
return true;
}
#1.5 write a method to replace all spaces in string with '%20'
#tips: i think the '%20' should be "%20", first count the number of the space, then get a new array
# donot return the local pointer, it will cause illegal access
//C/C++
char * newstr;
void replace_space(string str){
int count = 0;
for(int i=0;i<str.length();i++)
if(str[i] == ' ')
count++;
new_length = str.length() + 2*count + 1;
newstr = (char *)malloc(new_length);
for(int i=0;i<new_length;i++){
if(str[i] == ' '){
newstr[i] = '%';
newstr[++i] = '2';
newstr[++i] = '0';
}
else{
newstr[i] = str[i];
}
}
}
#1.6 write a method to rotate a N*N matrix by 90 degrees
#tips: (i,j)---(j,m-i)
#1.7 write an algorithm if an element in an M*N matrix is 0,its entire row and column is set to 0
#tips:first scan the matrix to record the 0's position
// C/C++
void set_zero(char ** matrix,int m,int n){
bool is_zero[m][n] = {false};
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(matrix[i][j] == 0)
is_zero[i][j] = 0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(is_zero){
for(int p=0;p<m;p++)
matrix[p][j] = 0;
for(int q = 0;q<n;q++)
matrix[i][q] = 0;
}
}
#1.8 write code to test if a string is a substring of the rotation of another string
#tips: rotation: "waterbottle" is a rotation of "erbottlewat"
//whether str2 is a substring of str1, the rotation in this question means counterclockwise rotation
//complexity O(2*length(str1) * length(str2))
//by using kmp algorithm, the complexity is O(2*length(str1))
bool is_substring(string str1, string str2){
int length = 2* str1.length();
char str[length];
for(int i=0;i<length/2;i++){
char[i] = str1[length/2-i-1];
char[length -i -1] = str1[length/2-i-1];
}
for(int i=0;i<length;i++)
for(int j=i;j<str2.length();j++){
if(char[i] != str2[j])
continue;
if(j == str2.length() - 1)
return true;
}
return false;
}