-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacter , Array and Strings Questions
138 lines (116 loc) · 3.26 KB
/
Character , Array and Strings Questions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//Count SUbstrings
class Solution {
public:
int expandAroundIndex(string s, int left, int right) {
int count = 0;
//jab tak match karega, tab tak count increment kardo and i piche and j aaage kardo
while(left >= 0 && right <s.length() && s[left] == s[right] ) {
count++;
left--;
right++;
}
return count;
}
int countSubstrings(string s) {
int totalCount = 0;
int n = s.length();
for(int center=0; center<n; center++) {
//odd
int oddKaAns = expandAroundIndex(s, center, center);
totalCount = totalCount + oddKaAns;
//even
int evenKaAns = expandAroundIndex(s,center,center+1);
totalCount = totalCount + evenKaAns;
}
return totalCount;
}
};
//Find Minimum Distance
class Solution {
public:
int findMinDifference(vector<string>& timePoints) {
//step1: convert time string into minutes integer value
vector<int> minutes;
for(int i=0; i<timePoints.size(); i++) {
string curr = timePoints[i];
int hours = stoi( curr.substr(0,2) );
int min = stoi( curr.substr(3,2) );
int totalMinutes = hours*60 + min;
minutes.push_back(totalMinutes);
}
//Step2: sort
sort(minutes.begin(), minutes.end());
//Step3: difference and calculate min diff
int mini = INT_MAX;
int n = minutes.size();
for(int i=0; i<n-1; i++) {
int diff = minutes[i+1] - minutes[i];
mini = min(mini, diff);
}
//something missing - THIS IS THE GAME
int lastDiff1 = (minutes[0] + 1440) - minutes[n-1];
int lastDiff2 = minutes[n-1] - minutes[0];
int lastDiff = min(lastDiff1, lastDiff2);
mini = min(mini, lastDiff);
return mini;
}
};
//Remove Duplicate Strings
class Solution {
public:
string removeDuplicates(string s) {
string ans ="";
int i = 0;
while( i < s.length() ) {
if( ans.length() > 0 && ans[ans.length() - 1] == s[i]) {
ans.pop_back();
}
else {
ans.push_back(s[i]);
}
i++;
}
return ans;
}
};
//Remove Occurences
class Solution {
public:
string removeOccurrences(string s, string part) {
int pos = s.find(part);
while(pos != string::npos) {
s.erase(pos, part.length());
pos = s.find(part);
}
return s;
}
};
//Valid Palindrome
class Solution {
public:
bool checkPalindrome(string s, int i, int j ) {
while(i <= j) {
if(s[i] != s[j] )
return false;
i++;
j--;
}
return true;
}
bool validPalindrome(string s) {
int i = 0;
int j = s.length() - 1;
while(i<=j) {
if(s[i] != s[j]) {
//ek baar i ko remove, ek baar j ko remove
return checkPalindrome(s,i+1, j) || checkPalindrome(s, i, j-1);
}
else {
//s[i] == s[j]
i++;
j--;
}
}
return true;
}
};