Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7fc4417

Browse files
Strings Questions
1 parent 3ad4c45 commit 7fc4417

File tree

10 files changed

+232
-0
lines changed

10 files changed

+232
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎#6 - Strings/0-strings-basics.js‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Strings in Javascript
2+
3+
// Creating Strings
4+
const string = new String("Subscribe to RoadsideCoder!");
5+
6+
// using double quotes
7+
const doubleQuote = "Subscribe to RoadsideCoder!";
8+
9+
// using single quotes
10+
const singleQuote = 'RoadsideCoder!';
11+
12+
// using backticks
13+
const backticks = `
14+
Subscribe to RoadsideCoder!
15+
Like this Video
16+
Share this video
17+
`;
18+
19+
// Template Literals
20+
const templateLiteral = `Subscribe to ${singleQuote}!`;
21+
22+
// Tagged Literals
23+
console.log`hello ${"world"} how are ${"you"}`
24+
25+
// Tagged Literals Example function
26+
function test (string, youtubeChannel, person) {
27+
console.log(string,youtubeChannel, person)
28+
return;
29+
}
30+
31+
const channel = "RoadsideCoder"
32+
const name = "Piyush"
33+
test`Subscribe to ${channel} run by ${name}`
34+
35+
// Strings length
36+
const str = "Subscribe to RoadsideCoder!";
37+
str.length
38+
39+
// Accessing Characters
40+
str[3]
41+
str.charAt(3)
42+
43+
// Looping
44+
for (let i = 0; i < str.length; i++) {
45+
// console.log(str[i]);
46+
}
47+
48+
// Modifying Strings
49+
str[3] = "b" // can't do this
50+
str.replace("s","b") // replaces first instance of that character or word
51+
str.replaceAll("e","c")// replaces all instance of that character or word
52+
53+
str.concat(" and share this video") // joins 2 strings
54+
const newStr = " Hello World "
55+
newStr.trim() // removes spaces from beginning and end
56+
57+
// Searching
58+
str.indexOf("Coder") // index of a character or word's first appearance
59+
60+
str.lastIndexOf("e") // index of a character or word's last appearance
61+
str.indexOf("e") // index of first appearance of "e"
62+
63+
str.startsWith("S")
64+
str.endsWith("!")
65+
66+
// Extracting Substrings
67+
str.substring(13, 26)
68+
str.slice(-14, -1)
69+
70+
// Converting Case
71+
const toBeConverted = {name:"Piyush"}
72+
String(toBeConverted) // [object Object]
73+
JSON.stringify(toBeConverted) // '{"name":"Piyush"}'
74+
75+
str.toUpperCase() // converts to uppercase
76+
str.toLowerCase() // converts to lowercase
77+
78+
str.charCodeAt(0) // returns ASCII Code
79+
String.fromCharCode(65) // returns character from ASCII Code
80+
81+
// String Comparison
82+
const str1 = "apple"
83+
const str2 = "apple"
84+
85+
str1.localeCompare(str2) // compare strings
86+
87+
str.includes("Subscribe") // checks if a word exists inside a string
88+
89+
// Splitting and Joining Strings
90+
str.split("e")
91+
92+
const arr = ["apple", "banana"]
93+
arr.join(" and ")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Ques 1 : Truncate the text
2+
// Write a function called truncate that checks the length of a given string - str, and if
3+
// it surpasses a specified maximum length, maxlength, it replaces the end of the string
4+
// with the ellipsis character "..." so that the length matches the maximum length
5+
6+
// Input: str = "Subscribe to RoadsideCoder" , maxlength=9
7+
// Output: "Subscribe..."
8+
9+
function truncate(str,maxlength) {
10+
if (str.length > maxlength)
11+
return str.slice(0, maxlength) + "..."
12+
else return str
13+
}
14+
15+
console.log(truncate("Subscribe to RoadsideCoder", 9));

‎#6 - Strings/2-palindrome.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Ques 2 - Palindrome Number
2+
// An integer is a palindrome when it reads the same forward and backward.
3+
4+
// Input: x = 121 ----->>>>> Output: true
5+
// Input: x = 10 ----->>>>> Output: false
6+
7+
var isPalindrome = function (x) {
8+
return x < 0 ? false : x === +x.toString().split("").reverse().join("");
9+
};
10+
11+
const res = isPalindrome(10);
12+
console.log(res);
13+
14+
// 121 => "121" => ["1","2","1"] => ["1","2","1"] => "121"

‎#6 - Strings/3-hamming-distance.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Ques 3 : Hamming Distance
2+
// Given two strings x and y, return the Hamming distance between them.
3+
4+
// Input: x = "hello", y = "hwllr"
5+
// Output: 2
6+
// Explanation:
7+
// (hello)
8+
// (hwllr)
9+
// ↑ ↑
10+
11+
function hammingDistance(x,y) {
12+
if (x.length !== y.length) {
13+
throw new Error("Strings must be of same length")
14+
}
15+
16+
let distance = 0;
17+
18+
for (let i = 0; i < x.length; i++){
19+
if (x[i] !== y[i]) {
20+
distance++
21+
}
22+
}
23+
24+
return distance
25+
}
26+
27+
console.log(hammingDistance("hello","hwllr"));
28+
29+
// Variation 2: Given two integers x and y, return the Hamming distance between thier bits.
30+
31+
// Input: x = 1, y = 4
32+
// Output: 2
33+
// Explanation:
34+
// 1 (0 0 0 1)
35+
// 4 (0 1 0 0)
36+
// ↑ ↑
37+
38+
function hammingDistance(x, y) {
39+
x=x.toString(2)
40+
y=y.toString(2)
41+
42+
if (x.length < y.length) {
43+
while(x.length!==y.length) x="0"+x
44+
} else {
45+
while(x.length!==y.length) y="0"+y
46+
}
47+
48+
let distance = 0;
49+
50+
for (let i = 0; i < x.length; i++){
51+
if (x[i] !== y[i]) {
52+
distance++
53+
}
54+
}
55+
56+
return distance
57+
}
58+
59+
console.log(hammingDistance(2,9));

‎#6 - Strings/4-valid-anagram.js‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Ques 4 - Valid Anagram
2+
// An Anagram is a word or phrase formed by rearranging the letters of
3+
// a different word or phrase, using all the original letters exactly once.
4+
5+
// Input: (s = "anagram"), (t = "nagaram"); ----->>>>> Output: true;
6+
// Input: (s = "rat"), (t = "car"); ----->>>>> Output: false;
7+
8+
// 1st Approach
9+
const isAnagram1 = function (s, t) {
10+
s = s.split("").sort().join("");
11+
t = t.split("").sort().join("");
12+
13+
return s === t;
14+
};
15+
16+
// anagram => [a,n,a,g,r,a,m] => [a,a,a,m,n,r] => aaamnr
17+
// nagaram => [n,a,g,a,r,a,m] => [a,a,a,m,n,r] => aaamnr
18+
19+
const isAnagram = function (s, t) {
20+
if (s.length !== t.length) return false;
21+
22+
let obj1 = {};
23+
let obj2 = {};
24+
25+
for (let i = 0; i < s.length; i++) {
26+
obj1[s[i]] = (obj1[s[i]] || 0) + 1;
27+
obj2[t[i]] = (obj2[t[i]] || 0) + 1;
28+
}
29+
30+
for (const key in obj1) {
31+
if (obj1[key] !== obj2[key]) return false;
32+
}
33+
34+
return true;
35+
};
36+
37+
console.log(isAnagram("anagram", "nagarm"));
38+
39+
// rat / tar
40+
41+
// {
42+
// r: 1,
43+
// a: 1,
44+
// t:1
45+
// }
46+
47+
// {
48+
// t: 1,
49+
// a: 1,
50+
// r:1
51+
// }

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /