generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem17.cs
More file actions
58 lines (50 loc) · 1.58 KB
/
Problem17.cs
File metadata and controls
58 lines (50 loc) · 1.58 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/decode-ways/">Decode Ways</see>.
/// </summary>
public static class Problem17
{
/// <summary>
/// A message containing letters from A-Z can be encoded into numbers using the following mapping:
/// 'A' -> "1".
/// 'B' -> "2".
/// ...
/// 'Z' -> "26".
/// To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways).
/// For example, "11106" can be mapped into:
/// * "AAJF" with the grouping (1 1 10 6).
/// * "KJF" with the grouping (11 10 6).
/// Given a string s containing only digits, return the number of ways to decode it.
/// Time complexity: O(n).
/// Space complexity: O(1).
/// </summary>
/// <param name="s">String to traverse.</param>
/// <returns>Number of ways to decode the input string.</returns>
public static int NumDecodings(string s)
{
if (s.Length == 0 || s[0] == '0')
{
return 0;
}
var second = 1;
for (int i = 1, first = 1; i < s.Length; i++)
{
var count = 0;
if (s[i - 1] != '0' && ((s[i - 1] - '0') * 10) + s[i] - '0' <= 26)
{
count += first;
}
if (s[i] != '0')
{
count += second;
}
if (count == 0)
{
return 0;
}
first = second;
second = count;
}
return second;
}
}