-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1047-remove-all-adjacent-duplicates-in-string.js
85 lines (64 loc) · 3.79 KB
/
1047-remove-all-adjacent-duplicates-in-string.js
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
// 1047. Remove All Adjacent Duplicates In String
// https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 68 ms, faster than 95.21% of JavaScript online submissions
// Memory Usage: 40.2 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {string} S
// * @return {string}
// */
// const removeDuplicates = S => {
// const a = S.split('');
// let end = -1;
// for (let i = 0; i < a.length; i++)
// if (0 <= end && a[end] === a[i]) end--;
// else a[++end] = a[i];
// return a.slice(0, end + 1).join('');
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 664 ms, faster than 8.35% of JavaScript online submissions
// Memory Usage: 123.8 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {string} str
// * @return {string}
// */
// const removeDuplicates = str => {
// for (let i = 0; i < str.length - 1; i++)
// if (str[i] === str[i + 1])
// return removeDuplicates(str.slice(0, i).concat(str.slice(i + 2)));
// return str;
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 80 ms, faster than 71.84% of JavaScript online submissions
// Memory Usage: 42.3 MB, less than 100.00% of JavaScript online submissions
/**
* @param {string} str
* @return {string}
*/
const removeDuplicates = str => {
let res = '';
for (let i = 0; i < str.length; i++)
if (str[i] === str[i + 1]) i++;
else res += str[i];
return str.length === res.length ? res : removeDuplicates(res);
};
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import { strictEqual } from 'assert';
strictEqual(removeDuplicates('abbaca'), 'ca');
// Explanation: For example, in "abbaca" we could remove "bb" since
// the letters are adjacent and equal, and this is the only possible
// move. The result of this move is that the string is "aaca",
// of which only "aa" is possible, so the final string is "ca".
strictEqual(removeDuplicates(''), '');
strictEqual(removeDuplicates('a'), 'a');
strictEqual(removeDuplicates('aa'), '');
strictEqual(removeDuplicates('aaa'), 'a');
strictEqual(removeDuplicates('aabbccddeeffgg'), '');
strictEqual(removeDuplicates('aabccadda'), 'b');
strictEqual(removeDuplicates('abaca'), 'abaca');
strictEqual(
removeDuplicates(
'miepavtabopghtcivgoguhgpjhiepbofvuvggfnpjllomlvuibrnulpegacebqwswjjfsevmnpcathldtkomiubnisfawbshbmswnjlcdocowvqhwivullaqkvfkeewriauouartlhotuctwhwlvecgaolbctivbdjrsqfwacvsnpjhouvappgpdskbcfhqjggpminhcffljiljfmcekudkjnugejfucwilfvdeaosmoppujofaurvantovgdclqcfqsssvmscbsnhogkcasckdbtiuovevsramwpcojqrkqqkukuaddidifmkdrjpfvcnmihwqudfqsrgeoleefuwmmgnvlgcugouelnjidaqggafhmoskmdnlfirngbqhfsfcfcfiwqkunrkfcseluhoshagnlhoneedllcflcrboofpuckusgcfroqgdcbpwskvnndrmorekebuffpnrkhrufwijbufetfjjadjlebclwigwdcmpvdjlqqlomjtooiagsiwadswcqqsclbwosdmarbooernmrfvtnnrfokbwksjaeucdscowqfgopgwqptffioviduvwwloqlodcrtdogogtkolursittcvfuewnrsmshfadnkswtqssstkfajosstdwrwgvmhuqvkguvlqovotakwhwhsgbcvffpsthlpfpwgsscmcbgohsilwgknhwkpfdiurhtlcsahuhvmlkte',
),
'miepavtabopghtcivgoguhgpjhiepbofvuvfnpjomlvuibrnulpegacebqwswfsevmnpcathldtkomiubnisfawbshbmswnjlcdocowvqhwivuaqkvfkwriauouartlhotuctwhwlvecgaolbctivbdjrsqfwacvsnpjhouvagpdskbcfhqjpminhcljiljfmcekudkjnugejfucwilfvdeaosmoujofaurvantovgdclqcfqsvmscbsnhogkcasckdbtiuovevsramwpcojqrukuaidifmkdrjpfvcnmihwqudfqsrgeolfuwgnvlgcugouelnjidaqafhmoskmdnlfirngbqhfsfcfcfiwqkunrkfcseluhoshagnlhondcflcrbfpuckusgcfroqgdcbpwskvdrmorekebupnrkhrufwijbufetfadjlebclwigwdcmpvdjomjtiagsiwadswcsclbwosdmarbernmrfvtrfokbwksjaeucdscowqfgopgwqptioviduvloqlodcrtdogogtkolursicvfuewnrsmshfadnkswtqstkfajotdwrwgvmhuqvkguvlqovotakwhwhsgbcvpsthlpfpwgcmcbgohsilwgknhwkpfdiurhtlcsahuhvmlkte',
);