Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression

Introduction

This theme

This theme, regular expression Ruby For example, if you put together WWWWBBWBBB in WB, it becomes WBWB, and you can see that it can be made into one color in 3 times. This kind of string processing is easy to solve using regular expressions.

ruby.rb


s = gets.chomp
s.gsub!(/W+/, "W")
s.gsub!(/B+/, "B")
puts s.size - 1

W + represents one or more consecutive ** W ** characters. Python

python.py


import re

s = input()
s = re.sub(r'W+', "W", s)
s = re.sub(r'B+', "B", s)
print(len(s) - 1)

If you want to use regular expressions in * Python *, you need ʻimport re`. Perl

perl.pl


chomp (my $s = <STDIN>);
$s =~ s/W+/W/g;
$s =~ s/B+/B/g;
print length($s) - 1, "\n";

Java

java.java


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        sc.close();
        s = s.replaceAll("W+", "W");
        s = s.replaceAll("B+", "B");
        System.out.println(s.length() - 1);
    }
}
Ruby Python Perl Java
Code length 71 Byte 97 Byte 87 Byte 310 Byte
Execution time 36 ms 38 ms 21 ms 239 ms
memory 10076 KB 4468 KB 640 KB 35160 KB

Summary

Referenced site

Recommended Posts

Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solve AtCoder ABC166 with python
Solve AtCoder ABC 186 with Python
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Solve ABC163 A ~ C with Python
Solve ABC168 A ~ C with Python
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
AtCoder ABC168 A case expression solved in Ruby and Python
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Solve AtCoder ABC168 with python (A ~ D)
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Solve AtCoder 167 with python
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
Solving with Ruby AtCoder ABC110 C String Manipulation
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
Benchmark for C, Java and Python with prime factorization
Solve Atcoder ABC176 (A, B, C, E) in Python
Regular expression manipulation with Python
ABC188 C problem with python3
ABC187 C problem with python
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
Scraping with Node, Ruby and Python
Solve ABC166 A ~ D with Python
Solve Atcoder ABC169 A-D in Python
String replacement with Python regular expression
Solve ABC036 A ~ C in Python
Solve ABC037 A ~ C in Python
AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression
[AtCoder commentary] Win the ABC165 C problem "Many Requirements" with Python!
List split and join strings with split and join (Perl / PowerShell / Java / Kotlin / Python)
Overlapping regular expressions in Python and Java
Solve ABC175 A, B, C in Python