Data Structure String In JavaScript | Frontend Interview Helper
Published: 2023-08-21 10:19:55

## The basics | Inbuilt Function of Js

### How to declare a String in JS

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692575417987/033f6f86-47bb-4039-bd6e-0bed8431cdb6.png align="center")

### Accessing Characters

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94csyawplx8bgtx2naw7.png)

### Accessing Characters

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbgosi1rs7e3u6w3ervi.png)

### Looping a string

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xh4p57as8y6b00yp6wy.png)
### Modifying Strings

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfnal1p7xzj3lv9t1cpz.png)
### Concat & Trim (Trim has only removed side spaces | didn't remove space in between )

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q242secihzzmxy9njxdu.png)
### Searching in String

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/58pi89ez7mnj35n5lv83.png)
### Took a part from a string ( Slice, Subdtring)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttc2yrl7suith5843vvj.png)

### Convert Case ( Str -> number | Num -> Str | etc..)
This is important because if we Typecast an Object in Js it gives a [ Object Object ] simple mines gives us its type

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfakruux94qxq8k4620u.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcrm8yha94gndpcgoze1.png)
The Right way to do that

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9vbl0y2k94c1rt856lx.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ese9iisnp7b6crigoti.png)
### Convert a String

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76vejh5gqwdl4zcuuu23.png)
### Check Ascii value or Convert to Ascii Value

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94b44sr2x4fxhy1o592t.png)

### String Compare

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/906snvyfuns0e62wlypz.png)
`it returns 0 if it is the same otherwise -1`
### To check whether something is included in String or not



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drydmeqlq6aw0u31n9yg.png)

`It returns True | False values`
### Splitting and Joining Strings

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmdy3iw6uya5vhon6dvz.png)
`It breaks the string from " " (space) occurs`




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j36zfzfno99olnha7hw7.png)

# Finally The Main Data Structure & Algorithm Questions Start.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rt71vwfyzra21kneznxh.png)


### My Solution:
`const mainString = '    Hay Good Morning Brother';
const maxLenght = 10;

if (mainString.length >= maxLenght) {
  let trimedvalue = mainString.trim();
  let finalVersionOfString = trimedvalue.replaceAll(" ", "");
  console.log(`${finalVersionOfString.slice(0, 10)}...`);
} else {
  console.log(mainString);
}
// Output : HayGoodMor...`




### Piyush's Solution:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jprjzx1ged1f3czfdvte.png)
## Q2: Plaindrome:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ncxs48uajusdp1x6x1g.png)
### My Solution:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgrvlcxvwud0b4yxd1cb.png)

I add comments with ChatGPT (I think it is a better approach because sometimes interviewers don't allow to use inbuilt functions of a language )
## quick Question
**Can Someone Explain?
Hey, I tried my best and approached this question in five different ways using JavaScript. The average runtime is around 140ms. However, when I attempted the same question in Java, it only took around 9ms. How is this possible? Why is there such a significant difference? Could someone please explain?**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otqk2i7dpkpyzo22qat9.png)
## Question 03: 461. Hamming Distance

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/537s4t51tro3edq0rzd3.png)
A quick tip: Convert a number to binary just | let num = 10; num.toString(2) --> that 2 refers base^2
`I don't know why on leetcode in this program time limit exceeds.`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikvczcu6ft6an5hhqha1.png)
## Here is the optimized Solution.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhiq8i8coumyxt5n610z.png)
## Question 04: 242. Valid Anagram

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6y0qc0yguuzkzk2zaw3.png)
### Solution: (UnOptimized ) Easy one:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2tk36z7owaqa5s8jau2.png)
### Solution: (Optimized ):

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fkau8dsabx45m1xeghi.png)
# Bonus:
Top 10 most asked frontend interview questions from string


 Reverse a String

Check Palindrome

String Anagrams

Longest Substring Without Repeating Characters

String Compression

Implement strStr()

Valid Parentheses

Longest Common Prefix

Group Anagrams

Count and Say

# Thank you So Much For Reading if you get some knowledge from this BLOG Connect with me on LinkedIn


Linkedin[Follow](https://www.linkedin.com/in/sameer-faridi/)
Github[Follow] (https://github.com/sameerkali/)

#### Made by ~~Lust~~ Love By Sameer Faridi (21 April 2023)