Shortest Distance to a Character
The problem is to find the shortest distance from each character in the string s
to the character c
.


The first loop finds the indices of character c
in the string s
. The second loop iterates through the string and calculates the absolute distance from each index to the closest occurrence of character c
. The result is a list containing these distances for each index in the string.
Identifying Problem Isomorphism
“Shortest Distance to a Character” can be approximately mapped to “K Closest Points to Origin”.
Here’s the reasoning:
In both problems, we need to calculate the distance from a specific point (in one case, a character in a string, and in the other, the origin in a Cartesian plane). For the “Shortest Distance to a Character”, you are given a string and a character. The goal is to find the shortest distance from every character in the string to the target character. Similarly, in “K Closest Points to Origin”, you’re given a list of points on a plane, and you need to find the K closest points to the origin.
Despite these similarities, these problems operate in different domains: one in strings, the other in a 2dimensional plane. This leads to differences in the calculation of distance. However, both problems require an understanding of distance measurement and processing a sequence to obtain the desired result.
“Shortest Distance to a Character” is simpler because it involves linear distance calculation in a onedimensional array (the string). “K Closest Points to Origin” requires calculating Euclidean distance in a twodimensional space and sorting points based on that distance, which adds complexity to the problem.


Problem Classification
This problem falls under the string manipulation and arrays.
‘What’ Components:
 Input: The function takes a string ’s’ and a character ‘c’ that occurs in s.
 Output: The function returns an array of integers of the same length as ’s’. Each element at index ‘i’ in the output array represents the shortest distance from index ‘i’ in the string ’s’ to the closest occurrence of character ‘c’.
 Task: The problem requires us to calculate the distance from each character in the string to the nearest occurrence of a given character.
This is an algorithmic problem where we need to perform a calculation for each element in the string. The solution requires a solid understanding of how to work with arrays and strings, and may also require an understanding of a specific algorithmic technique, such as twopointer or dynamic programming techniques. It is a singleinput, singleoutput problem.
Language Agnostic Coding Drills
 Dissection of the Code:
The given Python code snippet implements a solution to find the shortest distance of each character in a string from a specified character. It covers the following coding concepts:
a. Defining classes and functions: The use of the ‘class’ and ‘def’ keywords to define a class and a function, respectively.
b. Basic data types and variables: ’s’, ‘c’, ’n’, ‘ioc’, ‘ans’, ’m’, ’left’, and ‘right’ are all variables storing different types of data (lists, integers, and strings).
c. Control Flow: It contains loops (for loop), conditional statements (if), and control flow structures.
d. List operations: Creating, appending to, and indexing lists.
e. Builtin functions: Usage of builtin functions like ’len()’, ‘range()’, ‘min()’, and ‘abs()’.
f. Objectoriented programming: The concept of a class, defining methods within a class, and calling methods using the self keyword.
Coding Concepts/Drills in Order of Increasing Difficulty:
a. Defining classes and functions: These are basic programming constructs and are used to organize code into logical, manageable, and reusable parts.
b. Basic data types and variables: This is the next level, understanding different types of data that can be stored and manipulated.
c. Control Flow: Learning how to control the flow of code execution using loops and conditional statements is a critical step in becoming a proficient programmer.
d. List operations: Knowing how to create and manipulate lists is key for solving many problems.
e. Builtin functions: Learning to use builtin functions can greatly simplify code and reduce redundancy.
f. Objectoriented programming: This is a more advanced concept, requiring understanding of classes, objects, methods, and principles of encapsulation and abstraction.
ProblemSolving Approach:
The problemsolving strategy used in this problem involves storing the indices of the target character, then iterating over the string again to calculate the shortest distance of each character to the target character. Here is how the identified coding drills contribute to the overall solution:
a. Defining classes and functions: The code is organized into a class and two methods, providing a clear structure and making the code reusable.
b. Basic data types and variables: Variables store the string, the target character, the list of target character indices, the results, and various counters.
c. Control Flow: A loop iterates over the string to find the indices of the target character, then another loop calculates the shortest distances.
d. List operations: The list of indices is created and updated, and then used to calculate distances.
e. Builtin functions: These simplify the code, for example by calculating list lengths, generating ranges of numbers for the loops, and finding the minimum distance.
f. Objectoriented programming: The use of a class and methods allows the solution to be easily reused and tested.
Targeted Drills in Python
PythonBased Coding Drills for Each Identified Concept:
a. Defining classes and functions:
1 2 3
class MyClass: def my_function(self): print("Hello, world!")
b. Basic data types and variables:
1 2 3
my_string = "Hello, world!" my_int = 42 my_list = [1, 2, 3]
c. Control Flow:
1 2 3
for i in range(10): if i % 2 == 0: print(i)
d. List operations:
1 2 3
my_list = [1, 2, 3] my_list.append(4) first_element = my_list[0]
e. Builtin functions:
1 2 3
my_list = [1, 2, 3] length = len(my_list) minimum = min(my_list)
f. Objectoriented programming:
1 2 3
class MyClass: def my_function(self, my_parameter): self.my_variable = my_parameter
ProblemSpecific Coding Drills:
a. Finding the indices of a specific character in a string:
1 2 3
s = "hello" c = 'l' indices = [i for i in range(len(s)) if s[i] == c]
b. Calculating the minimum absolute difference between a number and a list of numbers:
1 2 3
number = 5 numbers = [1, 2, 7, 9] min_diff = min(abs(number  num) for num in numbers)
These drills are essential for our problem because they encapsulate the key steps of the solution: finding the positions of the target character and calculating the shortest distances to these positions.
 Integrating the Drills:
First, we define our class and function (drill 1a). Inside the function, we initialize our variables (drill 1b). We then use a loop (drill 1c) and list operations (drill 1d) to find the indices of the target character (drill 2a). We use another loop (drill 1c) to iterate over the string again, calculating the minimum distance of each character to the target character (drill 2b) and storing the results in our answer list (drill 1d). Throughout, we use builtin functions to simplify the code (drill 1e), and we use objectoriented programming principles to structure our solution (drill 1f).