Skip to content

Commit ae97742

Browse files
committed
796 and 3163
1 parent 58c3d32 commit ae97742

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
| 783 | Minimum Distance Between BST Nodes | [Ruby](./algorithms/ruby/0783-minimum-distance-between-bst-nodes.rb) [Python3](./algorithms/python3/0783-minimum-distance-between-bst-nodes.py) | Easy |
271271
| 785 | Is Graph Bipartite? | [Ruby](./algorithms/ruby/0785-is-graph-bipartite.rb) | Medium |
272272
| 790 | Domino and Tromino Tiling | [Ruby](./algorithms/ruby/0790-domino-and-tromino-tiling.rb) | Medium |
273+
| 796 | Rotate String | [Ruby](./algorithms/ruby/0796-rotate-string.rb) | Easy |
273274
| 799 | Champagne Tower | [Ruby](./algorithms/ruby/0799-champagne-tower.rb) | Medium |
274275
| 802 | Find Eventual Safe States | [Ruby](./algorithms/ruby/0802-find-eventual-safe-states.rb) | Medium |
275276
| 808 | Soup Servings | [Ruby](./algorithms/python3/0808-soup-servings.py) | Medium |
@@ -515,6 +516,7 @@
515516
| 2842 | Difference Between Ones and Zeros in Row and Column | [Ruby](./algorithms/ruby/2842-difference-between-ones-and-zeros-in-row-and-column.rb) | Medium |
516517
| 2849 | Determine if a Cell Is Reachable at a Given Time | [Ruby](./algorithms/ruby/2849-determine-if-a-cell-is-reachable-at-a-given-time) | Medium |
517518
| 2966 | Divide Array Into Arrays With Max Difference | [Ruby](./algorithms/ruby/2966-divide-array-into-arrays-with-max-difference) | Medium |
519+
| 3163 | String Compression III | [Ruby](./algorithms/ruby/3163-string-compression-iii.rb) | Medium |
518520

519521
### Database
520522
| # | Title | Solution | Difficulty |

algorithms/ruby/0796-rotate-string.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
# 796. Rotate String
4+
# Easy
5+
# https://leetcode.com/problems/rotate-string
6+
7+
=begin
8+
Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.
9+
A shift on s consists of moving the leftmost character of s to the rightmost position.
10+
* For example, if s = "abcde", then it will be "bcdea" after one shift.
11+
12+
Example 1:
13+
Input: s = "abcde", goal = "cdeab"
14+
Output: true
15+
Example 2:
16+
Input: s = "abcde", goal = "abced"
17+
Output: false
18+
19+
Constraints:
20+
* 1 <= s.length, goal.length <= 100
21+
* s and goal consist of lowercase English letters.
22+
=end
23+
24+
# @param {String} s
25+
# @param {String} goal
26+
# @return {Boolean}
27+
def rotate_string(s, goal)
28+
return false if s.length != goal.length
29+
(s + s).include?(goal)
30+
end
31+
32+
# **************** #
33+
# TEST #
34+
# **************** #
35+
36+
require "test/unit"
37+
class Test_rotate_string < Test::Unit::TestCase
38+
def test_
39+
assert_equal true, rotate_string("abcde", "cdeab")
40+
assert_equal false, rotate_string("abcde", "abced")
41+
end
42+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
# 3163. String Compression III
4+
# Medium
5+
# https://leetcode.com/problems/string-compression-iii/description/
6+
7+
=begin
8+
Given a string word, compress it using the following algorithm:
9+
* Begin with an empty string comp. While word is not empty, use the following operation:
10+
* Remove a maximum length prefix of word made of a single character c repeating at most 9 times.
11+
* Append the length of the prefix followed by c to comp.
12+
Return the string comp.
13+
14+
Example 1:
15+
Input: word = "abcde"
16+
Output: "1a1b1c1d1e"
17+
Explanation:
18+
Initially, comp = "". Apply the operation 5 times, choosing "a", "b", "c", "d", and "e" as the prefix in each operation.
19+
For each prefix, append "1" followed by the character to comp.
20+
21+
Example 2:
22+
Input: word = "aaaaaaaaaaaaaabb"
23+
Output: "9a5a2b"
24+
Explanation:
25+
Initially, comp = "". Apply the operation 3 times, choosing "aaaaaaaaa", "aaaaa", and "bb" as the prefix in each operation.
26+
* For prefix "aaaaaaaaa", append "9" followed by "a" to comp.
27+
* For prefix "aaaaa", append "5" followed by "a" to comp.
28+
* For prefix "bb", append "2" followed by "b" to comp.
29+
30+
Constraints:
31+
* 1 <= word.length <= 2 * 105
32+
* word consists only of lowercase English letters.
33+
=end
34+
35+
# @param {String} word
36+
# @return {String}
37+
def compressed_string(word)
38+
word.gsub(/(.)\1{,8}/) { _1.size.to_s + $1 }
39+
end
40+
41+
# **************** #
42+
# TEST #
43+
# **************** #
44+
45+
require "test/unit"
46+
class Test_compressed_string < Test::Unit::TestCase
47+
def test_
48+
assert_equal "1a1b1c1d1e", compressed_string("abcde")
49+
assert_equal "9a5a2b", compressed_string("aaaaaaaaaaaaaabb")
50+
end
51+
end

0 commit comments

Comments
 (0)