Skip to content

Commit 4ecf41a

Browse files
rearrage string problem
1 parent 7f1a5a7 commit 4ecf41a

File tree

4 files changed

+138
-2
lines changed

4 files changed

+138
-2
lines changed

exercises/linkedlist/checkPalindromeLinkedList.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
// Implement a function to check if a linked list is a palindrome.
1+
/**
2+
* Given a singly linked list, determine if it is a palindrome.
3+
4+
Example 1:
5+
6+
Input: 1->2
7+
Output: false
8+
Example 2:
9+
10+
Input: 1->2->2->1
11+
Output: true
12+
Follow up:
13+
Could you do it in O(n) time and O(1) space?
14+
* @param {LinkedList} list
15+
* @return {boolean}
16+
*/
217

318
function checkPalindrome(list) {
419
// Find the midpoint of the linked list
@@ -42,3 +57,39 @@ function reverseList(node) {
4257

4358
return previous;
4459
}
60+
61+
/**
62+
* Time Complexity: O(N)
63+
* Space Complexity: O(1)
64+
*/
65+
66+
// Approach 2
67+
const checkPalindrome = list => {
68+
if (!list.head) {
69+
return false;
70+
}
71+
72+
const arrayList = [];
73+
let node = list.head;
74+
while (node) {
75+
arrayList.push(node.val);
76+
node = node.next;
77+
}
78+
79+
let start = 0, end = arrayList.length - 1;
80+
81+
while (start <= end) {
82+
if (arrayList[start] !== arrayList[end]) {
83+
return false;
84+
}
85+
start++;
86+
end--;
87+
}
88+
89+
return true;
90+
}
91+
92+
/**
93+
* Time Complexity: O(N)
94+
* Space Complexity: O(N)
95+
*/

exercises/linkedlist/hasCycle.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Given a linked list, determine if it has a cycle in it.
3+
4+
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
5+
6+
7+
8+
Example 1:
9+
10+
Input: head = [3,2,0,-4], pos = 1
11+
Output: true
12+
Explanation: There is a cycle in the linked list, where tail connects to the second node.
13+
14+
Input: head = [1,2], pos = 0
15+
Output: true
16+
Explanation: There is a cycle in the linked list, where tail connects to the first node.
17+
18+
Input: head = [1], pos = -1
19+
Output: false
20+
Explanation: There is no cycle in the linked list.
21+
22+
function ListNode(val) {
23+
this.val = val;
24+
this.next = null;
25+
}
26+
@param {ListNode} (head)
27+
@return {boolean}
28+
*/
29+
const hasCycle = head => {
30+
if (!head || !head.next) {
31+
return false;
32+
}
33+
34+
let slow = head;
35+
let fast = head.next;
36+
37+
while (slow !== fast) {
38+
if (fast === null || fast.next === null) {
39+
return false;
40+
}
41+
slow = slow.next;
42+
fast = fast.next.next;
43+
}
44+
45+
return true;
46+
}
47+
/**
48+
* Time Complexity: O(N)
49+
* Space Complexity: O(1)
50+
*/

exercises/linkedlist/intersectionLinkedList.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ const getIntersectionNode = (headA, headB) => {
88
}
99

1010
return (ptrA === ptrB && ptrA !== null) ? ptrA : null;
11-
}
11+
}
12+
/**
13+
* Time Complexity: O (m + n);
14+
* Space Complexity: O(1)
15+
*/

exercises/strings/rearrangeString.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Given a string with lowercase, uppercase and numbers, rearrange it so that uppercase letters are first,
3+
* then lowercase and then numbers. It should maintain the original order.
4+
5+
Input- "cdBnC52c" output - "BCcdnc52"
6+
Input - "123abA" output - "Aab123"
7+
8+
Rearrange it in o(n) in one pass without using extra space.
9+
*/
10+
11+
const rearrangeString = str => {
12+
let lower = '';
13+
let upper = '';
14+
let num = '';
15+
16+
let index = 0;
17+
18+
while (index < str.length) {
19+
if (!isNaN(str[index] * 1)) {
20+
num = num + str[index];
21+
} else if (str[index] == str[index].toUpperCase()) {
22+
upper += str[index];
23+
} else if (str[index] == str[index].toLowerCase()) {
24+
lower += str[index];
25+
}
26+
27+
index++;
28+
}
29+
30+
return upper + lower + num;
31+
}

0 commit comments

Comments
 (0)