forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1165-SingleRowKeyboard.cs
More file actions
31 lines (27 loc) · 886 Bytes
/
1165-SingleRowKeyboard.cs
File metadata and controls
31 lines (27 loc) · 886 Bytes
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
//-----------------------------------------------------------------------------
// Runtime: 76ms
// Memory Usage: 23.1 MB
// Link: https://leetcode.com/submissions/detail/326440843/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _1165_SingleRowKeyboard
{
public int CalculateTime(string keyboard, string word)
{
var keyMapping = new Dictionary<char, int>();
for (int i = 0; i < keyboard.Length; i++)
keyMapping.Add(keyboard[i], i);
var current = 0;
var result = 0;
foreach (var ch in word)
{
result += Math.Abs(current - keyMapping[ch]);
current = keyMapping[ch];
}
return result;
}
}
}