Skip to content

Commit

Permalink
Cracking-the-Coding-Interview-Cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
pmbhatiya committed Jul 7, 2021
0 parents commit 0e7fa3d
Show file tree
Hide file tree
Showing 52 changed files with 3,820 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Ch-1--[Arrays and Strings]/1.1 Is_Unique.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include<bits/stdc++.h>
using namespace std;
bool isUniqueChar(string str){
if(str.length()>128){
return false;
}
bool *charset=new bool[128];
int val;
for(int i=0;i<str.length();i++){
val=int(str[i]);
if(charset[val]){
return false;
}else{
charset[val]=true;
}
}

return true;
}
int main(){
string s1="hello";
string s2="normal";
string s3="abcdefghijklmno";
bool flag;
flag=isUniqueChar(s3);

if(flag){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
return 0;
}
44 changes: 44 additions & 0 deletions Ch-1--[Arrays and Strings]/1.2 Check Permutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<bits/stdc++.h>
using namespace std;
//bool Check_permutation(string source,string destination){
// if(source.length()!=destination.length()){
// return false;
// }
// sort(source.begin(),source.end());
// sort(destination.begin(),destination.end());
// return (source==destination);
//
//}

bool Check_permutation(string source,string destination){
if(source.length()!=destination.length()){
return false;
}
int *charset=new int[128];
for(int i=0;i<source.length();i++){
charset[int(source[i])]++;
}
int charData;
for(int j=0;j<destination.length();j++){
charData=int(destination[j]);
charset[charData]--;

if(charset[charData]<0){
return false;
}
}
return true;
}
int main(){
string s1="hello";
string s2="olleh";
bool flag;
flag=Check_permutation(s1,s2);
if(flag){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}

return 0;
}
35 changes: 35 additions & 0 deletions Ch-1--[Arrays and Strings]/1.3 URLify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<bits/stdc++.h>
using namespace std;
string URLify(string source,int trueLength){
int spaces=0,index=0,i=0;
for(i=0;i<trueLength;i++){
if(source[i]==' '){
spaces+=1;
}
}
index=trueLength+(spaces*2);
if(trueLength<source.length()){
source[trueLength]='\0';
}
for(i=trueLength-1;i>=0;i--){
if(source[i]==' '){
source[index-1]='0';
source[index-2]='2';
source[index-3]='%';
index=index-3;
}else{
source[index-1]=source[i];
index--;

}

}
return source;
}
int main(){
string s1="Mr John Smith ";
int trueLength=13;
cout<<URLify(s1,trueLength);

return 0;
}
33 changes: 33 additions & 0 deletions Ch-1--[Arrays and Strings]/1.4 Palindrome Permutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include<bits/stdc++.h>
using namespace std;

bool permuatationPalindromeCheck(string str){
int *charset=new int[128];
int oddNo=0,temp=0;

memset(charset,0,128*sizeof(int));
for(int i=0;i<str.length();i++){
if(str[i]==' ')
continue;
temp=int(str[i]);
charset[temp]++;
if(charset[temp]%2==1){
oddNo++;
}else{
oddNo--;
}
}
cout<<oddNo<<endl;
return (oddNo<=1);
}
int main(){
string s1="tact coa";
bool flag;
flag=permuatationPalindromeCheck(s1);
if(flag){
cout<<"True"<<endl;
}else{
cout<<"False"<<endl;
}
return 0;
}
100 changes: 100 additions & 0 deletions Ch-1--[Arrays and Strings]/1.5 One Away.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include<bits/stdc++.h>
using namespace std;
//three type of modification can perform on string
// 1. insert -->>other string have one greter length
// 2. delete -->other string less than one length
// 3. replace (modify) -->both string same length than replace


//bool oneReplace(string source,string check){
// bool differanceCheck=false;
// for(int i=0;i<source.length();i++){
// if(source[i]!=check[i]){
// if(differanceCheck){
// return false;
// }
// differanceCheck=true;
// }
// }
// return true;
//}
//bool oneInsert(string source,string check){
// int index=0,index2=0;
//
// string s1=source.length()<check.length()?source:check;
// string s2=source.length()<check.length()?check:source;
// while(index<s1.length() && index2<s2.length()){
// if(s1[index]!=s2[index2]){
// if(index!=index2){
// return false;
// }
// index2++;
//
// }else{
// index++;
// index2++;
//
// }
// }
// return true;
//}
//bool oneaway(string source,string check){
// if(source.length()==check.length()){
// return oneReplace(source,check);
// }else if(source.length()-1==check.length()){ //true than it can be one away insert
// return oneInsert(source,check);
// }else if(source.length()+1==check.length()){ //true than it can be one away modify(delete)
// return oneInsert(source,check);
// }
//
//return false;
//}
bool oneaway(string source,string check){
int s1Len=source.length(),s2Len=check.length();
if(abs(s1Len-s2Len)>1){
return false;
}
string s1=s1Len<s2Len?source:check;
string s2=s1Len<s2Len?check:source;
s1Len=s1.length();
s2Len=s2.length();
int index=0,index2=0;
bool differance=false;
bool equalLength=false;
if(s1Len==s2Len){
equalLength=true;
}
while(index<s1Len && index2<s2Len){
if(s1[index]!=s2[index2]){

if(differance) return false;
differance=true;

if(equalLength) index++;
}else{
index++;
}
index2++;
}
return true;
}
int main(){

string s1="pale"; //source
string s2="pales";//source

string c1="ple"; //check
string c2="pale"; //check
string c3="bale"; //check
string c4="bae"; //check
string c5="ale"; //check
bool flag;
flag=oneaway(s1,c5);

if(flag){
cout<<"True"<<endl;
}else{
cout<<"False"<<endl;
}
return 0;
}
41 changes: 41 additions & 0 deletions Ch-1--[Arrays and Strings]/1.6 String Compression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<bits/stdc++.h>
using namespace std;
int compressedLen(string str){
int len=0;
int countContinue=0;
for(int i=0;i<str.length();i++){
countContinue++;
if(i+1>=str.length() || str[i]!=str[i+1]){
len+=to_string(countContinue).length()+1;
countContinue=0;
}


}
return len;
}
string stringComprison(string source){

int finalLen=compressedLen(source);
if(finalLen>=source.length()) return source;

string compress="";
int conCount=0;

for(int i=0;i<source.length();i++){
conCount++;
if(i+1>=source.length() || source[i]!=source[i+1]){
compress.append(1,source[i]);
compress.append(to_string(conCount));
conCount=0;
}
}
return compress;
}
int main(){

string s1="aaacccwweeer";
cout<<stringComprison(s1);

return 0;
}
57 changes: 57 additions & 0 deletions Ch-1--[Arrays and Strings]/1.7 Rotate Matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include<bits/stdc++.h>
using namespace std;

bool rotate90(int *mat,int rows,int cols){
int n=rows;
for(int layer=0;layer<n/2;layer++){

int first=layer;
int last=n-1-layer;
for(int i=first;i<last;i++){
int offset=i-first;
int top=mat[first*n+i]; //save top

//left->top
mat[first*n+i]=mat[(last-offset)*n+first];

//bootom->left
mat[(last-offset)*n+first]=mat[last*n+(last-offset)];

//right->bottom
mat[last*n+(last-offset)]=mat[i*n+last];

//top->right
mat[i*n+last]=top;

}

}

for(int x=0;x<rows;x++){
for(int j=0;j<cols;j++){
cout<<mat[x*4+j]<<" ";
}
cout<<endl;
}
return true;
}

int main(){

int mat[4][4]={{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
int n,rows,cols;
n=sizeof(mat)/sizeof(int);
rows=sizeof(mat)/sizeof(mat[0]);
cols=n/rows;
// cout<<n<<" "<<rows<<" "<<cols<<endl;
bool flag;
flag=rotate90(*mat,rows,cols);
if(!flag){
cout<<"Not Possible Rotation"<<endl;
}

return 0;
}
Loading

0 comments on commit 0e7fa3d

Please sign in to comment.