-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlineEncoding.R
More file actions
executable file
·59 lines (55 loc) · 1.52 KB
/
lineEncoding.R
File metadata and controls
executable file
·59 lines (55 loc) · 1.52 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
# Given a string, return its encoding defined as follows:
#
# First, the string is divided into the least possible number of disjoint
# substrings consisting of identical characters for example, "aabbbc" is divided
# into ["aa", "bbb", "c"] Next, each substring with length greater than one is
# replaced with a concatenation of its length and the repeating character for
# example, substring "bbb" is replaced by "3b" Finally, all the new strings are
# concatenated together in the same order and a new string is returned.
#
# Example
#
# For s = "aabbbc", the output should be lineEncoding(s) = "2a3bc".
#
# Input/Output
#
# [execution time limit] 5 seconds (r)
#
# [input] string s
#
# String consisting of lowercase English letters.
#
# Guaranteed constraints: 4 ≤ s.length ≤ 15.
#
# [output] string
#
# Encoded version of s.
s = "bbjaadlkjdl"
s = "abbcabb"
lineEncoding <- function(s) {
s2 = strsplit(s,"")[[1]]
if (uniqueN(s2) == length(s2)) {
return(s)
}
outputString = c()
digit = 1;
for (ind in 1:(length(s2) - 1)) {
if (s2[ind+1] != s2[ind]) {
if (digit > 1) {
outputString <- c(outputString,digit,s2[ind])
} else {
outputString <- c(outputString,s2[ind])
}
if (ind == (length(s2) - 1)) {
outputString <- c(outputString,s2[ind+1])
}
digit = 1;
} else {
digit = digit + 1;
if (ind == (length(s2) - 1)) {
outputString <- c(outputString,digit,s2[ind])
}
}
}
return(paste(outputString,collapse = ""))
}