Skip to content

Commit 708e542

Browse files
committed
Added 4 design solutions
1 parent 1ff38ef commit 708e542

4 files changed

+205
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class StringIterator {
2+
int letterPointer = 0;
3+
int numberPointer = letterPointer + 1;
4+
int counter;
5+
char c;
6+
String s;
7+
StringBuilder num;
8+
9+
public StringIterator(String compressedString) {
10+
s = compressedString;
11+
c = s.charAt(letterPointer);
12+
13+
num = new StringBuilder();
14+
while (numberPointer<s.length() && Character.isDigit(s.charAt(numberPointer))) {
15+
num.append(s.charAt(numberPointer));
16+
numberPointer++;
17+
}
18+
counter = Integer.parseInt(num.toString());
19+
20+
letterPointer = numberPointer;
21+
numberPointer = letterPointer + 1;
22+
}
23+
24+
public char next() {
25+
if (counter == 0) {
26+
if (numberPointer <= s.length()-1) {
27+
c = s.charAt(letterPointer);
28+
num = new StringBuilder();
29+
while (numberPointer<s.length() && Character.isDigit(s.charAt(numberPointer))) {
30+
num.append(s.charAt(numberPointer));
31+
numberPointer++;
32+
}
33+
counter = Integer.parseInt(num.toString()) - 1;
34+
35+
letterPointer = numberPointer;
36+
numberPointer = letterPointer + 1;
37+
}
38+
else {
39+
c = ' ';
40+
}
41+
}
42+
else {
43+
counter--;
44+
}
45+
46+
return c;
47+
}
48+
49+
public boolean hasNext() {
50+
if (counter == 0) {
51+
if (numberPointer > s.length()-1) {
52+
return false;
53+
}
54+
}
55+
return true;
56+
}
57+
}
58+
59+
/**
60+
* Your StringIterator object will be instantiated and called as such:
61+
* StringIterator obj = new StringIterator(compressedString);
62+
* char param_1 = obj.next();
63+
* boolean param_2 = obj.hasNext();
64+
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class TwoSum {
2+
3+
/** Initialize your data structure here. */
4+
List<Integer> list;
5+
Map<Integer, Integer> map;
6+
public TwoSum() {
7+
list = new ArrayList<>();
8+
map = new HashMap<>();
9+
}
10+
11+
/** Add the number to an internal data structure.. */
12+
public void add(int number) {
13+
if (map.containsKey(number)) {
14+
map.put(number, map.get(number) + 1);
15+
}
16+
else {
17+
map.put(number, 1);
18+
list.add(number);
19+
}
20+
}
21+
22+
/** Find if there exists any pair of numbers which sum is equal to the value. */
23+
public boolean find(int value) {
24+
for (int num : list) {
25+
int diff = value - num;
26+
if (diff != num && map.containsKey(diff)) {
27+
return true;
28+
}
29+
30+
if (diff == num && map.get(diff) > 1) {
31+
return true;
32+
}
33+
}
34+
35+
return false;
36+
}
37+
}
38+
39+
/**
40+
* Your TwoSum object will be instantiated and called as such:
41+
* TwoSum obj = new TwoSum();
42+
* obj.add(number);
43+
* boolean param_2 = obj.find(value);
44+
*/

Medium/Plus One Linked List.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode plusOne(ListNode head) {
11+
ListNode rev = reverse(head);
12+
ListNode prev = null;
13+
int carry = 1;
14+
ListNode curr = rev;
15+
while (curr != null) {
16+
int temp = curr.val + carry;
17+
if (temp > 9) {
18+
carry = 1;
19+
temp = temp - 10;
20+
}
21+
else {
22+
carry = 0;
23+
}
24+
25+
prev = curr;
26+
27+
curr.val = temp;
28+
curr = curr.next;
29+
}
30+
31+
if (carry != 0) {
32+
prev.next = new ListNode(carry);
33+
}
34+
35+
ListNode ans = reverse(rev);
36+
37+
return ans;
38+
}
39+
40+
private ListNode reverse(ListNode node) {
41+
ListNode curr = node;
42+
ListNode prev = null;
43+
ListNode next = null;
44+
45+
while (curr != null) {
46+
next = curr.next;
47+
curr.next = prev;
48+
prev = curr;
49+
curr = next;
50+
}
51+
52+
return prev;
53+
}
54+
}

Medium/Unique Word Abbrevation.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class ValidWordAbbr {
2+
3+
Map<String, String> map;
4+
public ValidWordAbbr(String[] dictionary) {
5+
map = new HashMap<>();
6+
7+
for (String word : dictionary) {
8+
String abbr = getAbbr(word);
9+
if (map.containsKey(abbr)) {
10+
if (!map.get(abbr).equals(word)) {
11+
map.put(abbr, "");
12+
}
13+
}
14+
else {
15+
map.put(abbr, word);
16+
}
17+
}
18+
}
19+
20+
public boolean isUnique(String word) {
21+
String abbr = getAbbr(word);
22+
return !map.containsKey(abbr) || map.get(abbr).equals(word);
23+
}
24+
25+
private String getAbbr(String word) {
26+
if (word.length() <= 2) {
27+
return word;
28+
}
29+
30+
int num = word.length() - 2;
31+
StringBuilder sb = new StringBuilder();
32+
sb.append(word.charAt(0)).append(String.valueOf(num)).append(word.charAt(word.length()-1));
33+
34+
return sb.toString();
35+
}
36+
}
37+
38+
39+
/**
40+
* Your ValidWordAbbr object will be instantiated and called as such:
41+
* ValidWordAbbr obj = new ValidWordAbbr(dictionary);
42+
* boolean param_1 = obj.isUnique(word);
43+
*/

0 commit comments

Comments
 (0)