- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the longest distance to a character in a given string using C#?
Create 2 different arrays leftDis and rightDis. The leftDis will store the value when moved from left direction. The rightDis will store the shortest value when moved from right. Whenever the character is met add the position of the character to the array. In the last step calculate the maximum of both the arrays.
Time Complexity − O(n)
Space Complexity − O(n)
Example
public class Arrays{ public int[] LongestDistanceToCharacter(string s, char c){ int stringLength = s.Length; int[] leftDis = new int[s.Length]; int[] rightDis = new int[s.Length]; leftDis = Enumerable.Range(0, s.Length).Select(n => int.MinValue).ToArray(); rightDis = Enumerable.Range(0, s.Length).Select(n => int.MaxValue).ToArray(); int count = int.MaxValue; for (int i = 0; i < rightDis.Length; i++){ if (s[i] == c){ count = 0; rightDis[i] = count; } else{ if (count != int.MaxValue){ count++; rightDis[i] = count; } } } count = int.MaxValue; for (int i = leftDis.Length - 1; i >= 0; i--){ if (s[i] == c){ count = 0; leftDis[i] = count; } else{ if (count != int.MaxValue){ count++; leftDis[i] = count; } } } int[] ans = new int[stringLength]; for (int i = 0; i < stringLength - 1; i++){ ans[i] = Math.Max(leftDis[i], rightDis[i]); } return ans; } } static void Main(string[] args){ Arrays s = new Arrays(); string ss = "lovecode"; char c = 'e'; var res = s.LongestDistanceToCharacter(ss, c); foreach (var item in res){ Console.WriteLine(item); } }
Output
[2147483647,2147483647,2147483647,0,3,2,3,0]
- Related Articles
- How to find the shortest distance to a character in a given string using C#?
- Find the index of the first unique character in a given string using C++
- C++ Program to find the Shortest Distance to a character
- How to find a unique character in a string using java?
- How to find the first character of a string in C#?
- Java program to find the Frequency of a character in a given String
- How to obtain the highest occurring character in a String using C#?
- Queries to find the last non-repeating character in the sub-string of a given string in C++
- Find the first repeated character in a string using C++.
- How to find the length of the longest substring from the given string without repeating the characters using C#?
- Finding the longest consecutive appearance of a character in another string using JavaScript
- Find one extra character in a string using C++.
- How to find the vowels in a given string using Java?
- How to find its first non-repeating character in a given string in android?
- How to match a character from given string including case using Java regex?

Advertisements