-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathReverseWords.kt
143 lines (124 loc) · 4.16 KB
/
ReverseWords.kt
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
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
*
* You are given a character array containing a set of words separated by whitespace.
* Your task is to modify that character array so that the words all appear in reverse order.
* Do this without using any extra space.
*/
@UseCommentAsDocumentation
fun swapWithoutExtraSpace(input: CharArray): CharArray {
// reverse the input inplace
input.reverse()
// swap each letter in the word
var start = 0
for (i in 0..input.size) {
val current = input.getOrNull(i) // loops till length not indices
if (current == ' ' || current == null) {
_swapLetters(input, start, i - 1)
start = i + 1
}
}
return input
}
/**
* Do it without having trailing or multiple white spaces. Move extra whitespaces to the end
*/
fun swapWithoutTrailingOrMultipleWhitespace(input: CharArray): CharArray {
// reverse the input inplace
input.reverse()
var wordsCount = 0
// swap each letter in the word
// keep record of where the swap starts from
var replaceStartsFrom = 0
for (i in 0..input.size) {
val current = input.getOrNull(i)
if (current == null) {
_swapLetters(input, replaceStartsFrom, i - 1)
} else {
if (!current.isWhitespace()) { // not whitespace so must be a word
wordsCount += 1
}
if (wordsCount == 0) { // only spaces till now
continue
}
if (wordsCount > 0 && current.isWhitespace()) { // words encountered and right now at whitespace
// swap words and previously encountered whitespace so that previous whitespaces are shifted to the end
_swapLetters(input, replaceStartsFrom, i - 1)
// move to the position of last word
// add 1 for the one whitespace in between two words
// and consider remaining whitespaces for swapping in later iteration
replaceStartsFrom += wordsCount + 1
wordsCount = 0
}
}
}
return input
}
private fun _swapLetters(arr: CharArray, startIndex: Int, endIndex: Int) {
var startFrom = startIndex
var endAt = endIndex
val till = (endIndex - startFrom) / 2
for (i in 0..till) {
val temp = arr[startFrom]
arr[startFrom] = arr[endAt]
arr[endAt] = temp
startFrom++
endAt--
}
}
fun main() {
run {
val input = charArrayOf('A', 'l', 'i', 'c', 'e', ' ', 'l', 'i', 'k', 'e', 's', ' ', 'B', 'o', 'b')
swapWithoutExtraSpace(input) shouldBe charArrayOf(
'B',
'o',
'b',
' ',
'l',
'i',
'k',
'e',
's',
' ',
'A',
'l',
'i',
'c',
'e'
)
}
run {
val input = "Algorithm repository"
swapWithoutExtraSpace(input.toCharArray()) shouldBe "repository Algorithm".toCharArray()
}
run {
val input = "Algorithm"
swapWithoutExtraSpace(input.toCharArray()) shouldBe "Algorithm".toCharArray()
}
run {
val input = "algorithm repository reverse words"
swapWithoutExtraSpace(input.toCharArray()) shouldBe "words reverse repository algorithm".toCharArray()
}
run {
val input = " reverse words "
swapWithoutTrailingOrMultipleWhitespace(input.toCharArray()) shouldBe "words reverse ".toCharArray()
}
run {
val input = " words "
swapWithoutTrailingOrMultipleWhitespace(input.toCharArray()) shouldBe "words ".toCharArray()
}
run {
val input = " reverse words "
swapWithoutTrailingOrMultipleWhitespace(input.toCharArray()) shouldBe "words reverse ".toCharArray()
}
run {
val input = " Alice likes Bob "
swapWithoutTrailingOrMultipleWhitespace(input.toCharArray()) shouldBe "Bob likes Alice ".toCharArray()
}
run {
val input = " "
swapWithoutTrailingOrMultipleWhitespace(input.toCharArray()) shouldBe " ".toCharArray()
}
}