Skip to content

Commit 7cc9dc1

Browse files
p114
1 parent edc7e60 commit 7cc9dc1

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

AllQuestions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,7 @@ example, given "hello world here", return "here world hello"
18321832
Follow-up: given a mutable string representation, can you perform this operation
18331833
in-place?
18341834

1835-
## Problem-114:waxing_crescent_moon:
1835+
## [Problem-114](src/main/java/in/ashwanik/dcp/problems/p91_120/p114):sunny:
18361836

18371837

18381838
> This problem was asked by Facebook.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ Solutions to the coding problems from [Daily coding problem](https://dailycoding
1111
|[P94](src/main/java/in/ashwanik/dcp/problems/p91_120/p94)|[P100](src/main/java/in/ashwanik/dcp/problems/p91_120/p100)|[P104](src/main/java/in/ashwanik/dcp/problems/p91_120/p104)|[P108](src/main/java/in/ashwanik/dcp/problems/p91_120/p108)|[P111](src/main/java/in/ashwanik/dcp/problems/p91_120/p111)|[P113](src/main/java/in/ashwanik/dcp/problems/p91_120/p113)|
1212

1313

14-
## **Facebook (15)**
14+
## **Facebook (16)**
1515
| | | | | | | | | | | | | | | |
1616
|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|
1717
|[P7](src/main/java/in/ashwanik/dcp/problems/p1_30/p7)|[P15](src/main/java/in/ashwanik/dcp/problems/p1_30/p15)|[P19](src/main/java/in/ashwanik/dcp/problems/p1_30/p19)|[P25](src/main/java/in/ashwanik/dcp/problems/p1_30/p25)|[P27](src/main/java/in/ashwanik/dcp/problems/p1_30/p27)|[P30](src/main/java/in/ashwanik/dcp/problems/p1_30/p30)|[P41](src/main/java/in/ashwanik/dcp/problems/p31_60/p41)|[P47](src/main/java/in/ashwanik/dcp/problems/p31_60/p47)|[P51](src/main/java/in/ashwanik/dcp/problems/p31_60/p51)|[P60](src/main/java/in/ashwanik/dcp/problems/p31_60/p60)|[P62](src/main/java/in/ashwanik/dcp/problems/p61_90/p62)|[P69](src/main/java/in/ashwanik/dcp/problems/p61_90/p69)|[P79](src/main/java/in/ashwanik/dcp/problems/p61_90/p79)|[P85](src/main/java/in/ashwanik/dcp/problems/p61_90/p85)|[P110](src/main/java/in/ashwanik/dcp/problems/p91_120/p110)|
18+
|[P114](src/main/java/in/ashwanik/dcp/problems/p91_120/p114)|
1819

1920

2021
## **Microsoft (11)**
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Given a string and a set of delimiters, reverse the words in the string while
2+
maintaining the relative order of the delimiters. For example, given
3+
"hello/world:here", return "here/world:hello"
4+
5+
Follow-up: Does your solution work for the following cases: "hello/world:here/",
6+
"hello//world:here"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package in.ashwanik.dcp.problems.p91_120.p114;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.Set;
6+
7+
class Solution {
8+
9+
String reverseWordsWithDelimiters(String sentence, Set<Character> delimiters) {
10+
if (sentence == null || sentence.isEmpty()) {
11+
return sentence;
12+
}
13+
14+
Deque<String> stack = new ArrayDeque<>();
15+
Deque<Character> queue = new ArrayDeque<>();
16+
17+
int start = -1;
18+
int end = -1;
19+
20+
for (int index = 0; index < sentence.length(); index++) {
21+
if (delimiters.contains(sentence.charAt(index))) {
22+
queue.offer(sentence.charAt(index));
23+
end = index - 1;
24+
if (start >= 0 && start < sentence.length() && end < sentence.length()) {
25+
stack.push(sentence.substring(start, index));
26+
}
27+
start = index + 1;
28+
} else {
29+
if (start == -1) {
30+
start = index;
31+
}
32+
}
33+
}
34+
if (start >= 0 && start < sentence.length()) {
35+
stack.push(sentence.substring(start));
36+
}
37+
String result = "";
38+
while (!stack.isEmpty()) {
39+
String delimiter = "";
40+
if (!queue.isEmpty()) {
41+
delimiter = queue.remove() + "";
42+
}
43+
result += stack.pop() + delimiter;
44+
}
45+
46+
return result;
47+
}
48+
49+
50+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package in.ashwanik.dcp.problems.p91_120.p114;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class SolutionTest {
11+
@Test
12+
void testReverseStringWithDelimiter() {
13+
Set<Character> set = new HashSet<>();
14+
set.add('/');
15+
set.add(':');
16+
assertEquals("here/world:hello", new Solution().reverseWordsWithDelimiters("hello/world:here", set));
17+
assertEquals("here/world:hello/", new Solution().reverseWordsWithDelimiters("hello/world:here/", set));
18+
}
19+
}

0 commit comments

Comments
 (0)