Skip to content

Commit 7a7f99c

Browse files
committed
isomorbhic srings question
Decorators topic added mixins in progress
1 parent 708111c commit 7a7f99c

File tree

8 files changed

+129
-1
lines changed

8 files changed

+129
-1
lines changed

D/decorators/Animal.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function log(target, name, descriptor) {
2+
const originalMethod = descriptor.value;
3+
descriptor.value = function (...args) {
4+
console.log(`Called ${name} with args: ${args}`);
5+
return originalMethod.apply(this, args);
6+
};
7+
return descriptor;
8+
}
9+
10+
class Example {
11+
@log
12+
greet(name) {
13+
return `Hello, ${name}!`;
14+
}
15+
}
16+
17+
const example = new Example();
18+
example.greet("World"); // Logs: Called greet with args: [ 'World' ]

D/decorators/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Decorators
2+
3+
A decorator is a design pattern that allows you to modify or extend the behavior of an object or function or class without changing its original code
4+
5+
*The core idea is that you "wrap" or "decorate" an object, class, or method to add extra features.*
6+
7+
- decorators are usually prefixed with @.

D/decorators/decorator.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

E/es7/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
**ES7 (ECMAScript 2016)**:
22
- **Exponentiation Operator (`**`)**: The exponentiation operator (`**`) was introduced to replace `Math.pow()`. It allows you to raise numbers to a power more easily.
3+
34
- **Array.prototype.includes**: A new method for arrays to check if an element exists in the array. It is more readable than `indexOf()`.

E/es8/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
**ES8 (ECMAScript 2017)**:
22

33
- **`async`/`await`**: Introduced as a cleaner way to handle asynchronous code by using promises in a more synchronous-like manner. It improves code readability and reduces callback hell.
4+
45
- **Object.entries() and Object.values()**: These methods provide an easy way to iterate over the properties and values of an object. `Object.entries()` returns an array of key-value pairs, while `Object.values()` returns an array of values.
6+
57
- **String padding methods (`padStart()` and `padEnd()`)**: These methods allow you to pad the beginning or the end of a string with specified characters to ensure a specific string length.
8+
69
- **Shared memory and atomics**: Introduced shared memory for use with Web Workers and atomics for controlling concurrent access to memory.

M/mixin/mixin.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const flyingMixin = {
2+
fly() {
3+
console.log(`${this.name} is flying!`);
4+
},
5+
};
6+
7+
const walkingMixin = {
8+
walk() {
9+
console.log(`${this.name} is walking!`);
10+
},
11+
};
12+
13+
class Bird {
14+
constructor(name) {
15+
this.name = name;
16+
}
17+
}
18+
19+
// Applying mixins
20+
Object.assign(Bird.prototype, flyingMixin, walkingMixin);
21+
22+
const eagle = new Bird("Eagle");
23+
eagle.fly(); // Output: Eagle is flying!
24+
eagle.walk(); // Output: Eagle is walking!

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313

14-
<script src="./leetcode-solutions/20-Valid_Parentheses.js"> </script>
14+
<script src="./leetcode-solutions/205-Isomorphic_Strings.js"> </script>
1515
</body>
1616

1717
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Given two strings s and t, determine if they are isomorphic.
3+
4+
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
5+
6+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
7+
*/
8+
9+
/**
10+
* @param {string} s
11+
* @param {string} t
12+
* @return {boolean}
13+
*/
14+
var isIsomorphic = function (s, t) {
15+
let map = new Map();
16+
17+
let ans = "";
18+
19+
for (let i = 0; i < s.length; i++) {
20+
let elementS = s[i];
21+
let elementT = t[i];
22+
// check if item already exists in map
23+
if (map.has(elementS)) {
24+
// if yes => then add value corresponding to it in ans
25+
ans += map.get(elementS);
26+
} else {
27+
// if no => add[same index] it to map => then add value to string
28+
if ([...map.values()].includes(elementT)) {
29+
return false;
30+
}
31+
map.set(elementS, elementT);
32+
ans+= elementT;
33+
}
34+
}
35+
console.log(ans);
36+
37+
if(ans === t) return true;
38+
39+
return false;
40+
41+
};
42+
43+
let s1 = "paper";
44+
let t1 = "title";
45+
let s2 = "foo";
46+
let t2 = "bar";
47+
let s3 = "egg";
48+
let t3 = "add";
49+
let s4 = "badc";
50+
let t4 = "baba";
51+
52+
// console.log(isIsomorphic(s1, t1));
53+
// console.log(isIsomorphic(s2, t2));
54+
// console.log(isIsomorphic(s3, t3));
55+
console.log(isIsomorphic(s4, t4));
56+
57+
58+
59+
// intution
60+
61+
/* question says ki humko ekk string se dusti string bana k dekhna hai ki ban rhi hai k nhi
62+
given ki hum second string k elements ko first string k elements se replacce krte jaye.
63+
64+
ek condition hai ki ek character ko ek value assign ho gy to vahi use hogi.
65+
66+
67+
iske liye we will use a map
68+
69+
agar map mai value hai to usko use karenge
70+
agar nhi hai to new use karenge
71+
72+
final jo string banegi usko second string se compare karenge for answer
73+
*/

0 commit comments

Comments
 (0)