Facebook
From hung, 1 Month ago, written in Dart.
Embed
Download Paste or View Raw
Hits: 127
  1. import 'dart:math';
  2.  
  3. class Solution {
  4.   List<int> shortestToChar(String s, String c) {
  5.     List<int> res = List.filled(s.length, 0);
  6.     int c_position = s.length;
  7.     for (int i = 0; i < s.length; i++) {
  8.       if (s[i] == c) {
  9.          c_positi
  10.       }
  11.       res[i] = (i - c_position).abs();
  12.     }
  13.     for (int i = s.length - 1; i >= 0; i--) {
  14.       if (s[i] == c) {
  15.         c_position = i;
  16.       }
  17.       res[i] = min(res[i], (i - c_position).abs());
  18.     }
  19.     return res;
  20.   }
  21. }
  22.  
  23. void main() {
  24.   String s = "aaaabaaa";
  25.   String c = "b";
  26.   print(Solution().shortestToChar(s, c));
  27. }
  28.