Skip to content

Commit 33cdd6b

Browse files
committed
feat: ✨ adding string manipulation problems
1 parent 8b3ef4f commit 33cdd6b

9 files changed

+118
-5
lines changed

code/dist/lengthOfLongestSubstring.js

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/dist/lengthOfLongestSubstring.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/dist/matching-strings.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/dist/matching-strings.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/dist/reverseInteger.js

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/dist/reverseInteger.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function lengthOfLongestSubstring(text: string): number {
2+
let maxLength = 0
3+
let substringText = ''
4+
const textList = text.split('')
5+
6+
if (text.length === 0) {
7+
return 0
8+
}
9+
10+
textList.forEach((char) => {
11+
if (!substringText.includes(char)) {
12+
substringText = substringText.concat(char)
13+
} else {
14+
substringText = char
15+
}
16+
17+
if (substringText.length > maxLength) {
18+
maxLength = substringText.length
19+
}
20+
})
21+
22+
return maxLength
23+
}
24+
25+
function mainLengthOfLongestSubstring() {
26+
const response = lengthOfLongestSubstring('abcabcbb')
27+
console.log(response)
28+
}
29+
30+
mainLengthOfLongestSubstring()

code/src/matching-strings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function matchingStrings(stringList: string[], queries: string[]): number[] {
1616
return resultsList
1717
}
1818

19-
function main() {
19+
function mainMatchingStrings() {
2020
let stringList: string[] = ['def', 'de', 'fgh']
2121
let queries: string[] = ['de', 'lmn', 'fgh']
2222

@@ -25,4 +25,4 @@ function main() {
2525
console.log(result)
2626
}
2727

28-
main()
28+
mainMatchingStrings()

code/src/reverseInteger.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function reverseInteger(x: number): number {
2+
const NEGATIVE_SIGN = '-'
3+
const LOW_INT_INTERVAL = -(2 ** 31)
4+
const UPPER_INT_INTERVAL = 2 ** 31 - 1
5+
6+
// Transform number in char list
7+
const stringNumber: string = x.toString()
8+
const stringNumberList: string[] = stringNumber.split('')
9+
10+
// Check if value is negative, if so, adds - to the end of the list
11+
const isNegative: boolean = x < 0
12+
13+
if (isNegative) {
14+
stringNumberList.push(NEGATIVE_SIGN)
15+
}
16+
17+
// Reverses the list and join into a string
18+
stringNumberList.reverse()
19+
const reversedString: string = stringNumberList.join('')
20+
21+
// Parses back to integer
22+
const numberResult = parseInt(reversedString)
23+
const isValidResult = numberResult >= LOW_INT_INTERVAL && numberResult <= UPPER_INT_INTERVAL
24+
25+
return isValidResult ? parseInt(reversedString) : 0
26+
}
27+
28+
function mainReverseInteger() {
29+
const response = reverseInteger(-123)
30+
console.log(response)
31+
}
32+
33+
mainReverseInteger()

0 commit comments

Comments
 (0)