-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.cpp
More file actions
69 lines (51 loc) · 1.82 KB
/
sample.cpp
File metadata and controls
69 lines (51 loc) · 1.82 KB
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
#include<bits/stdc++.h>
using namespace std;
string result(string s){
if(s.size()==0) return "";
for(int i=0;i<s.size();i++){
for(int j=i+1;j<s.size();j++){
if(abs(s[j]-s[i])==1 || abs(s[j]-s[i])==25){
cout<<abs(s[j]-s[i])<<endl;
s.erase(i,1);
s.erase(j-1,1);//to account for 1 removed char
}
}
}
return s;
}
int main(){
cout<<result("adcb");
// string s = "abc";
// cout<<s.erase(1,1);
return 0;
}
/*
Q2. Resulting String After Adjacent Removals
Medium
4 pt.
You are given a string s consisting of lowercase English letters.
You must repeatedly perform the following operation while the string s has at least two consecutive characters:
Remove the leftmost pair of adjacent characters in the string that are consecutive in the alphabet, in either order (e.g., 'a' and 'b', or 'b' and 'a').
Shift the remaining characters to the left to fill the gap.
Return the resulting string after no more operations can be performed.
Note: Consider the alphabet as circular, thus 'a' and 'z' are consecutive.
Example 1:
Input: s = "abc"
Output: "c"
Explanation:
Remove "ab" from the string, leaving "c" as the remaining string.
No further operations are possible. Thus, the resulting string after all possible removals is "c".
Example 2:
Input: s = "adcb"
Output: ""
Explanation:
Remove "dc" from the string, leaving "ab" as the remaining string.
Remove "ab" from the string, leaving "" as the remaining string.
No further operations are possible. Thus, the resulting string after all possible removals is "".
Example 3:
Input: s = "zadb"
Output: "db"
Explanation:
Remove "za" from the string, leaving "db" as the remaining string.
No further operations are possible. Thus, the resulting string after all possible removals is "db".
*/