- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0567-permutation-in-string.rb
46 lines (37 loc) · 835 Bytes
/
0567-permutation-in-string.rb
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
# frozen_string_literal: true
# 567. Permutation in String
# https://leetcode.com/problems/permutation-in-string
# @param {String} s1
# @param {String} s2
# @return {Boolean}
defcheck_inclusion(s1,s2)
returnfalseifs1.size > s2.size
s1_h=Hash.new(0)
s1.each_char{ |c| s1_h[c] += 1}
i=0
j=0
whilej < s2.size
ifs1_h[s2[j]] != 0
s1_h[s2[j]] -= 1
returntrueifj - i == s1.size - 1
j += 1
elsifi == j
i += 1
j += 1
else
s1_h[s2[i]] += 1
i += 1
end
end
false
end
# ********************#
# TEST #
# ********************#
require"test/unit"
classTest_check_inclusion < Test::Unit::TestCase
deftest_
assert_equaltrue,check_inclusion("ab","eidbaooo")
assert_equalfalse,check_inclusion("ab","eidboaoo")
end
end