-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (111 loc) · 4.88 KB
/
Copy pathindex.js
File metadata and controls
152 lines (111 loc) · 4.88 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
const solution = () => {
// const A = [1, 2, 3];
//const A = [-1, -2, -10000];
const A = [1, 2, 3, 6, 4, 1, 2, 4];
let orderedA = [0];
orderedA[0] = 0;
for (let i = 1; i < A.length + 2; i++) {
orderedA[i] = i;
}
//orderedA[orderedA.length] = orderedA.length ;
console.log('A :' + A + '\n');
console.log('Start orderedA :' + orderedA + '\n');
for (let i = 0; i < A.length; i++) {
console.log('A[' + i + '] =' + A[i] + '\n');
if (A[i] > 0) {
orderedA[A[i]] = 0;
}
}
console.log('After orderedA :' + orderedA + '\n');
for (let i = 1; i < orderedA.length; i++) {
if (orderedA[i] > 0) {
console.log('orderedA[' + i + '] =' + orderedA[i] + '\n');
return orderedA[i];
}
}
}; //solution
/*
shortest possible length of the compressed representation of a string
Strings with long blocks of repeating characters take much less space if kept in a compressed representation. To obtain the compressed representation, we replace each segment of equal characters in the string with the number of characters in the segment followed by the character(e.x we replace segment "CCCC" with 4C). To avoid increasing the size, we leave the one-letter segments unchanged(the compressed representation of "BC" is the same string - "BC"). For example, the compressed representation of the string "ABBBCCDDCCC" is "A3B2C2D3C", and the compressed representation of the string "AAAAAAAAAAABXXAAAAAAAAAA" is "11AB2X10A". Observe that, in the second example, if we removed the "BXX" segment from the middle of the word, we would obtain a much shorter compressed representation - "21A". In order to take advantage of this observation, we decided to modify the compression algorithm. Now before compression, we remove exactly K consecutive letters from the input string. We would like to know the shortest compressed form that we can generate this way.
Write a function:
that given a string S of length N and an integer K, returns the shortest possible length of the compressed representation of S after removing exactly K consecutive characters from S.
Example:
Given S = "AAAAAAAAAAABXXAAAAAAAAAA" and K=3, the function should return 3, because after removing "BXX" from S, we are left with "AAAAAAAAAAAAAAAAAAAAA", which compresses to a representation of length 3 - "21A".
Requirement:
N is an integer within the range [1...100,000];
K is an integer within the range [0...100,000];
K <= N;
string S consists only of uppercase letters (A-Z)
*/
const compressedString = (S, K) => {
let chars = S.split('');
let newS = chars[0];
let i = 0,
j = 0;
let currChar = 0,
nextChat = 0;
let counterCurr = 0,
counterNext = 0;
console.log(`compressedString input S = ${S} newS= ${newS}`);
for (i = 1; i < S.length; i++) {
}
return newS;
};
var val2 = compressedString('ABBBCCDDCCC', 3);
console.log('compressedString =' + val2 + '\n');
val2 = compressedString('AAAAAAAAAAABXXAAAAAAAAAA', 3);
console.log('compressedString =' + val2 + '\n');
/*
const shortestLength = (S, K) => {
let length = Number.MAX_SAFE_INTEGER;
console.log(`shortestLength input S = ${S} K= ${K}`);
if (K > S.length) {
return S.length;
}
let shortestStr = '';
let chars = S.split('');
console.log('shortestLength input chars = ' + chars + '\n');
chars.forEach((char, index, arr) => {
let tempStr = S.replace(S.substr(index, K), '');
console.log('tempStr= ' + tempStr + '\n');
let countObj = {};
tempStr.split('').forEach(char => {
countObj[char] === undefined ? (countObj[char] = 1) : countObj[char]++;
});
let ansStr = '';
for (const char in countObj) {
ansStr += `${countObj[char] === 1 ? '' : countObj[char]}${char}`;
}
if (ansStr.length < length) {
//shortestStr = `${ansStr}`;
shortestStr = ansStr;
}
length = ansStr.length < length ? ansStr.length : length;
console.log('shortestStr= ' + shortestStr + ' length = ' + length + '\n');
console.log(
'ansStr= ' + ansStr + ' ansStr.length = ' + ansStr.length + '\n'
);
});
console.log('END shortestStr= ' + shortestStr + ' length = ' + length + '\n');
return length;
};
// document.write(solution("ABBBCCDDCCC", 3))
// console.log(shortestLength('ABBBCCDDCCC', 3));
var val = shortestLength('ABBBCCDDCCC', 3);
console.log('shortestLength =' + val + '\n');
val = shortestLength('AAAAAAAAAAABXXAAAAAAAAAA', 3);
console.log('shortestLength =' + val + '\n');
val = shortestLength('ABCDDEFG', 2);
console.log('shortestLength =' + val + '\n');
*/
////////////////////////////////////////////////////////////////////
ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: http://bit.ly/CRA-PWA
serviceWorker.unregister();