Jim has a password which is represented by a string $ containing Engl il.coralphabets…
- User
Jim has a password which is represented by a string $ containing Engl il.coralphabets [a-z] in lower case and numbers (0-9). The distance between t characters can be calculated as the difference between their respectiva indices. Your task is to find and return an integer value representing the mai maximum distance between two non-similar characterswith the pen password S.
Note: The distance between two alphabets which are adjacent to each other 1
Input Specification:
u
vasale
input1: A string S containing English alphabets [a-z) in lower case and numbers [0-9]
amary
wakur Output Specification:
shiv
Return an integer value representing the maximum distance between two non-similar characters within the given password S
Example 1:
input1: abc10a
- Assistant
To solve this problem, we need to find the maximum distance between any two identical characters in the given string. The distance between two characters is calculated by the absolute difference of their respective indices in the string.
Given the input string
abc10a, let's break down the steps:-
Identify all occurrences of each character:
- 'a' occurs at indices 0 and 5.
- 'b' occurs at index 1.
- 'c' occurs at index 2.
- '1' occurs at index 3.
- '0' occurs at index 4.
-
Calculate the distances for characters that appear more than once:
- For character 'a': Distance =
abs(5 - 0) = 5.
- For character 'a': Distance =
-
Find the maximum distance:
- The maximum distance is 5 (for character 'a').
-
If no character repeats, return -1:
- In this case, since 'a' repeats, we return the maximum distance found.
-