Skip to content

Commit 0e7fa3d

Browse files
committed
Cracking-the-Coding-Interview-Cpp
0 parents commit 0e7fa3d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3820
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
bool isUniqueChar(string str){
4+
if(str.length()>128){
5+
return false;
6+
}
7+
bool *charset=new bool[128];
8+
int val;
9+
for(int i=0;i<str.length();i++){
10+
val=int(str[i]);
11+
if(charset[val]){
12+
return false;
13+
}else{
14+
charset[val]=true;
15+
}
16+
}
17+
18+
return true;
19+
}
20+
int main(){
21+
string s1="hello";
22+
string s2="normal";
23+
string s3="abcdefghijklmno";
24+
bool flag;
25+
flag=isUniqueChar(s3);
26+
27+
if(flag){
28+
cout<<"Yes"<<endl;
29+
}else{
30+
cout<<"No"<<endl;
31+
}
32+
return 0;
33+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
//bool Check_permutation(string source,string destination){
4+
// if(source.length()!=destination.length()){
5+
// return false;
6+
// }
7+
// sort(source.begin(),source.end());
8+
// sort(destination.begin(),destination.end());
9+
// return (source==destination);
10+
//
11+
//}
12+
13+
bool Check_permutation(string source,string destination){
14+
if(source.length()!=destination.length()){
15+
return false;
16+
}
17+
int *charset=new int[128];
18+
for(int i=0;i<source.length();i++){
19+
charset[int(source[i])]++;
20+
}
21+
int charData;
22+
for(int j=0;j<destination.length();j++){
23+
charData=int(destination[j]);
24+
charset[charData]--;
25+
26+
if(charset[charData]<0){
27+
return false;
28+
}
29+
}
30+
return true;
31+
}
32+
int main(){
33+
string s1="hello";
34+
string s2="olleh";
35+
bool flag;
36+
flag=Check_permutation(s1,s2);
37+
if(flag){
38+
cout<<"Yes"<<endl;
39+
}else{
40+
cout<<"No"<<endl;
41+
}
42+
43+
return 0;
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
string URLify(string source,int trueLength){
4+
int spaces=0,index=0,i=0;
5+
for(i=0;i<trueLength;i++){
6+
if(source[i]==' '){
7+
spaces+=1;
8+
}
9+
}
10+
index=trueLength+(spaces*2);
11+
if(trueLength<source.length()){
12+
source[trueLength]='\0';
13+
}
14+
for(i=trueLength-1;i>=0;i--){
15+
if(source[i]==' '){
16+
source[index-1]='0';
17+
source[index-2]='2';
18+
source[index-3]='%';
19+
index=index-3;
20+
}else{
21+
source[index-1]=source[i];
22+
index--;
23+
24+
}
25+
26+
}
27+
return source;
28+
}
29+
int main(){
30+
string s1="Mr John Smith ";
31+
int trueLength=13;
32+
cout<<URLify(s1,trueLength);
33+
34+
return 0;
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
bool permuatationPalindromeCheck(string str){
5+
int *charset=new int[128];
6+
int oddNo=0,temp=0;
7+
8+
memset(charset,0,128*sizeof(int));
9+
for(int i=0;i<str.length();i++){
10+
if(str[i]==' ')
11+
continue;
12+
temp=int(str[i]);
13+
charset[temp]++;
14+
if(charset[temp]%2==1){
15+
oddNo++;
16+
}else{
17+
oddNo--;
18+
}
19+
}
20+
cout<<oddNo<<endl;
21+
return (oddNo<=1);
22+
}
23+
int main(){
24+
string s1="tact coa";
25+
bool flag;
26+
flag=permuatationPalindromeCheck(s1);
27+
if(flag){
28+
cout<<"True"<<endl;
29+
}else{
30+
cout<<"False"<<endl;
31+
}
32+
return 0;
33+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
//three type of modification can perform on string
4+
// 1. insert -->>other string have one greter length
5+
// 2. delete -->other string less than one length
6+
// 3. replace (modify) -->both string same length than replace
7+
8+
9+
//bool oneReplace(string source,string check){
10+
// bool differanceCheck=false;
11+
// for(int i=0;i<source.length();i++){
12+
// if(source[i]!=check[i]){
13+
// if(differanceCheck){
14+
// return false;
15+
// }
16+
// differanceCheck=true;
17+
// }
18+
// }
19+
// return true;
20+
//}
21+
//bool oneInsert(string source,string check){
22+
// int index=0,index2=0;
23+
//
24+
// string s1=source.length()<check.length()?source:check;
25+
// string s2=source.length()<check.length()?check:source;
26+
// while(index<s1.length() && index2<s2.length()){
27+
// if(s1[index]!=s2[index2]){
28+
// if(index!=index2){
29+
// return false;
30+
// }
31+
// index2++;
32+
//
33+
// }else{
34+
// index++;
35+
// index2++;
36+
//
37+
// }
38+
// }
39+
// return true;
40+
//}
41+
//bool oneaway(string source,string check){
42+
// if(source.length()==check.length()){
43+
// return oneReplace(source,check);
44+
// }else if(source.length()-1==check.length()){ //true than it can be one away insert
45+
// return oneInsert(source,check);
46+
// }else if(source.length()+1==check.length()){ //true than it can be one away modify(delete)
47+
// return oneInsert(source,check);
48+
// }
49+
//
50+
//return false;
51+
//}
52+
bool oneaway(string source,string check){
53+
int s1Len=source.length(),s2Len=check.length();
54+
if(abs(s1Len-s2Len)>1){
55+
return false;
56+
}
57+
string s1=s1Len<s2Len?source:check;
58+
string s2=s1Len<s2Len?check:source;
59+
s1Len=s1.length();
60+
s2Len=s2.length();
61+
int index=0,index2=0;
62+
bool differance=false;
63+
bool equalLength=false;
64+
if(s1Len==s2Len){
65+
equalLength=true;
66+
}
67+
while(index<s1Len && index2<s2Len){
68+
if(s1[index]!=s2[index2]){
69+
70+
if(differance) return false;
71+
differance=true;
72+
73+
if(equalLength) index++;
74+
}else{
75+
index++;
76+
}
77+
index2++;
78+
}
79+
return true;
80+
}
81+
int main(){
82+
83+
string s1="pale"; //source
84+
string s2="pales";//source
85+
86+
string c1="ple"; //check
87+
string c2="pale"; //check
88+
string c3="bale"; //check
89+
string c4="bae"; //check
90+
string c5="ale"; //check
91+
bool flag;
92+
flag=oneaway(s1,c5);
93+
94+
if(flag){
95+
cout<<"True"<<endl;
96+
}else{
97+
cout<<"False"<<endl;
98+
}
99+
return 0;
100+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int compressedLen(string str){
4+
int len=0;
5+
int countContinue=0;
6+
for(int i=0;i<str.length();i++){
7+
countContinue++;
8+
if(i+1>=str.length() || str[i]!=str[i+1]){
9+
len+=to_string(countContinue).length()+1;
10+
countContinue=0;
11+
}
12+
13+
14+
}
15+
return len;
16+
}
17+
string stringComprison(string source){
18+
19+
int finalLen=compressedLen(source);
20+
if(finalLen>=source.length()) return source;
21+
22+
string compress="";
23+
int conCount=0;
24+
25+
for(int i=0;i<source.length();i++){
26+
conCount++;
27+
if(i+1>=source.length() || source[i]!=source[i+1]){
28+
compress.append(1,source[i]);
29+
compress.append(to_string(conCount));
30+
conCount=0;
31+
}
32+
}
33+
return compress;
34+
}
35+
int main(){
36+
37+
string s1="aaacccwweeer";
38+
cout<<stringComprison(s1);
39+
40+
return 0;
41+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
bool rotate90(int *mat,int rows,int cols){
5+
int n=rows;
6+
for(int layer=0;layer<n/2;layer++){
7+
8+
int first=layer;
9+
int last=n-1-layer;
10+
for(int i=first;i<last;i++){
11+
int offset=i-first;
12+
int top=mat[first*n+i]; //save top
13+
14+
//left->top
15+
mat[first*n+i]=mat[(last-offset)*n+first];
16+
17+
//bootom->left
18+
mat[(last-offset)*n+first]=mat[last*n+(last-offset)];
19+
20+
//right->bottom
21+
mat[last*n+(last-offset)]=mat[i*n+last];
22+
23+
//top->right
24+
mat[i*n+last]=top;
25+
26+
}
27+
28+
}
29+
30+
for(int x=0;x<rows;x++){
31+
for(int j=0;j<cols;j++){
32+
cout<<mat[x*4+j]<<" ";
33+
}
34+
cout<<endl;
35+
}
36+
return true;
37+
}
38+
39+
int main(){
40+
41+
int mat[4][4]={{1,2,3,4},
42+
{5,6,7,8},
43+
{9,10,11,12},
44+
{13,14,15,16}};
45+
int n,rows,cols;
46+
n=sizeof(mat)/sizeof(int);
47+
rows=sizeof(mat)/sizeof(mat[0]);
48+
cols=n/rows;
49+
// cout<<n<<" "<<rows<<" "<<cols<<endl;
50+
bool flag;
51+
flag=rotate90(*mat,rows,cols);
52+
if(!flag){
53+
cout<<"Not Possible Rotation"<<endl;
54+
}
55+
56+
return 0;
57+
}

0 commit comments

Comments
 (0)