Score of a String - Leetcode 3110 - Python
Skills:
Algorithm Basics90%
Key Takeaways
The video solves the Leetcode problem 3110, Score of a String, using Python. It calculates the score of a string by taking the absolute difference of ASCII values of every pair of adjacent characters.
Full Transcript
hey everyone welcome back and let's write some more neat code today so today let's solve the problem score of a string so we're given a string s with a bunch of characters we want to calculate the score of it and we can do that by taking for example on this string by taking the asy value of every pair of characters every adjacent pair of characters so we have one pair over here another pair over here and then another pair here and then this pair over here as well so we have four pairs we have five characters but we only have four pairs now for each pair to get the asy value of that character is pretty much a builtin function so H for example is 104 in terms of asky e is 101 now if you have no idea what this means just think of it like this you have a bunch of characters like in the alphabet to get the difference in terms of two characters uh like H and E to get the difference of the asy values of those two characters is the same as saying how many different like how many spots do we have to jump from one character to land at the other in the alphabet cuz the asky values are in alphabetical order so even if you know we didn't know what the asky value of this is we still kind of know that the difference between them is three so the difference here is also going to be three if you had no idea what asky values are you could create your own mapping you could call a zero this one this two this three Etc and you know you could store these in a hashmap or something and then you could solve the problem that way way it would still technically be constant memory to do this cuz it's 26 characters but we don't need to do that cuz in Python there is a built-in function called or which pretty much gets the asky value of a character so knowing that we are going to iterate over the input and we're going to have a pointer let's call it I and I'm going to iterate up until not the last but the second to last character because we have five characters in the input but we have four pairs we want to iterate for every PA so to compare two adjacent characters I can just say I with I + 1 and I can get the asky value of these two take the difference and then get the absolute value of that because the difference is three in this case but we could have subtracted it in the opposite order where the difference would have been -3 we want that value to always be positive so we take the absolute value and so we do this for the first pair next we shift I over here and then do the same for this pair and then do the same for this pair and same for the last pair and then we stop aggregating those values up in this example we'll end up with a 13 and you can see how they got to that down here but now I think it's time for us to code this up obviously we are able to solve this problem in linear time just by scanning through the input so I'm going to store the score in a result variable that's what I'm going to call it and that's what we're going to return and then in terms of iterating over the input we could use two separate pointers if we wanted to like with a while loop I'm going to keep it simple with a for loop we're going to go through the length of the string minus one though we don't want to visit the last position and then we're going to take the character at index I get the asky value of it take the character at index I + 1 get the asky value of that as well and then before we add this to the result let's go ahead and take the absolute value now we can add this to the result easiest way to do it is like this and now to run the code you can see it works and it's about as efficient as we can get it if you're preparing for coding interviews check out n code. thanks for watching and I'll see you soon
Original Description
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews
🧑💼 LinkedIn: https://www.linkedin.com/in/navdeep-singh-3aaa14161/
🐦 Twitter: https://twitter.com/neetcode1
⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf
Problem Link: https://leetcode.com/problems/score-of-a-string/
0:00 - Drawing Explanation
2:45 - Coding Explanation
leetcode 3110
#neetcode #leetcode #python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeetCodeIO · NeetCodeIO · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Leetcode 149 - Maximum Points on a Line - Python
NeetCodeIO
Design Linked List - Leetcode 707 - Python
NeetCodeIO
Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
NeetCodeIO
Design Browser History - Leetcode 1472 - Python
NeetCodeIO
Number of Good Paths - Leetcode 2421 - Python
NeetCodeIO
Flip String to Monotone Increasing - Leetcode 926 - Python
NeetCodeIO
Maximum Sum Circular Subarray - Leetcode 918 - Python
NeetCodeIO
Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
NeetCodeIO
Concatenated Words - Leetcode 472 - Python
NeetCodeIO
Data Stream as Disjoint Intervals - Leetcode 352 - Python
NeetCodeIO
LFU Cache - Leetcode 460 - Python
NeetCodeIO
N-th Tribonacci Number - Leetcode 1137
NeetCodeIO
Best Team with no Conflicts - Leetcode 1626 - Python
NeetCodeIO
Greatest Common Divisor of Strings - Leetcode 1071 - Python
NeetCodeIO
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
NeetCodeIO
Insert into a Binary Search Tree - Leetcode 701 - Python
NeetCodeIO
Delete Node in a BST - Leetcode 450 - Python
NeetCodeIO
Shuffle the Array (Constant Space) - Leetcode 1470 - Python
NeetCodeIO
Fruits into Basket - Leetcode 904 - Python
NeetCodeIO
Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
NeetCodeIO
Naming a Company - Leetcode 2306 - Python
NeetCodeIO
As Far from Land as Possible - Leetcode 1162 - Python
NeetCodeIO
Shortest Path with Alternating Colors - Leetcode 1129 - Python
NeetCodeIO
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
NeetCodeIO
Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
NeetCodeIO
Contains Duplicate II - Leetcode 219 - Python
NeetCodeIO
Path with Maximum Probability - Leetcode 1514 - Python
NeetCodeIO
Add to Array-Form of Integer - Leetcode 989 - Python
NeetCodeIO
Unique Paths II - Leetcode 63 - Python
NeetCodeIO
Minimum Distance between BST Nodes - Leetcode 783 - Python
NeetCodeIO
Design Hashmap - Leetcode 706 - Python
NeetCodeIO
Range Sum Query Immutable - Leetcode 303 - Python
NeetCodeIO
Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
NeetCodeIO
Middle of the Linked List - Leetcode 876 - Python
NeetCodeIO
Course Schedule IV - Leetcode 1462 - Python
NeetCodeIO
Single Element in a Sorted Array - Leetcode 540 - Python
NeetCodeIO
Capacity to Ship Packages - Leetcode 1011 - Python
NeetCodeIO
IPO - Leetcode 502 - Python
NeetCodeIO
Minimize Deviation in Array - Leetcode 1675 - Python
NeetCodeIO
Longest Turbulent Array - Leetcode 978 - Python
NeetCodeIO
Last Stone Weight II - Leetcode 1049 - Python
NeetCodeIO
Construct Quad Tree - Leetcode 427 - Python
NeetCodeIO
Find Duplicate Subtrees - Leetcode 652 - Python
NeetCodeIO
Sort an Array - Leetcode 912 - Python
NeetCodeIO
Ones and Zeroes - Leetcode 474 - Python
NeetCodeIO
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
NeetCodeIO
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
NeetCodeIO
Concatenation of Array - Leetcode 1929 - Python
NeetCodeIO
Symmetric Tree - Leetcode 101 - Python
NeetCodeIO
Check Completeness of a Binary Tree - Leetcode 958 - Python
NeetCodeIO
Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
NeetCodeIO
Find Peak Element - Leetcode 162 - Python
NeetCodeIO
Accounts Merge - Leetcode 721 - Python
NeetCodeIO
Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
NeetCodeIO
Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
NeetCodeIO
Number of Zero-Filled Subarrays - Leetcode 2348 - Python
NeetCodeIO
Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
NeetCodeIO
Sqrt(x) - Leetcode 69 - Python
NeetCodeIO
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
NeetCodeIO
Optimal Partition of String - Leetcode 2405 - Python
NeetCodeIO
More on: Algorithm Basics
View skill →
🎓
Tutor Explanation
DeepCamp AI