Search for character string B from character string A (duplicate count)

I want to search for a certain character string from a certain character string

I want to search for "s" from the following character string "t".


s = 'AA'
t = 'abdeeAAbAAAbfde'

Normally, I'm a fool who thought, "Hey, I can go with the count method."


s = 'AA'
t = 'abdeeAAbAAAbfde'
puts t.count(s)

#Expected output ➡︎ 3
#Actual output ➡︎ 5

No, I just count the number of A normally. With the count method, even if you give a set of character strings to the arguments, it seems that one character or one character will be searched.

I want to search for the string AA because it can be duplicated!

In other words

  1. First AA counting from the left
  2. The first AAA of AAA (AA ← this A)
  3. Last AA (A this → AA)

I want to count the above three. (Transferred ...! Lol)

What should I do

If you think about it by chewing, the string abdeeAAbAAAbfde Divide into ab bd de ee eA AA Ab bA AA AA Ab bf fd de It is good to verify whether it matches the character string AA, count if it is true, and not count if it is false.

※point Specify the range by making good use of the index of the character string

Actual code


s = 'AA'
t = 'abdeeAAbAAAbfde'

result = 0

(0..(t.size - s.size)).each do |i|
  substring = t.slice(i, s.size)

  if substring == s
    result += 1

puts result

#Expected output ➡︎ 3
#Actual output ➡︎ 3


What you need to know

size method

You can count strings by using it in the string class. Along with the length method (which counts the number of elements when used in the Array class).

slice method

If you specify a range with an argument, the character string of that range is returned.


I think the part of "how to cut out the necessary data" is the miso of this time. I think that the part that doesn't come out suddenly is not yet the head to create the algorithm. Hmmm difficult.

Recommended Posts

Search for character string B from character string A (duplicate count)
[Linux] When you want to search for a specific character string from multiple files
Character duplicate count (substring)
[Java] How to erase a specific character from a character string
Search list for duplicate elements
[Addition] A memo for dividing a character string containing multiple spaces
How to quickly count the frequency of appearance of characters from a character string in Python?
Search for English words from terminal
[Excel] Search for duplicate values (characters)
Django: Import a class from a string
Create a pandas Dataframe from a string.
Try to extract a character string from an image with Python3
Python-dynamically call a function from a string
How to extract the desired character string from a line 4 commands
A program that searches for a character string, and when the search character string is found, displays the character string from the beginning of the line to just before the search character string.
Outputs a line containing the specified character string from a text file
To receive a string from Julialang
[Python] How to invert a character string
Deletes after the specified character from the character string
[Java] Divide a character string by a specified character
Search for Pokemon haunting information from Twitter
Generate a class from a string in Python
How to make a judgment method to search for an arbitrary character in an array
Extract character by character from UTF-8 string using ICU
I tried to generate a random character string
Search for yourself from methods in Django's model
Create a datetime object from a string in Python (Python 3.3)
[Android] How to convert a character string to resourceId
How to create a function object from a string
A set for C language test cases. Character string including numbers, maximum value, minimum value
Try searching for a million character profile in Python
Character range / character string range
[Java] How to cut out a character string character by character
# Function that returns the character code of a string
A vulnerability diagnostician who is not involved in development wrote a string search code for text
Get the variable name of the variable as a character string.
[Python] How to expand variables in a character string
When handling a character string with the scanf function
I want to split a character string with hiragana
String search algorithm
Extract character by character from UTF-8 string considering invalid byte string
From building a multi-platform environment for Cpputest to tutorials
Output a character string with line breaks in PyYAML
Count the number of characters in a UTF-8 string
String Replacement of the case where the regular expression * of the character string search condition contains a line break.
I want to specify a file that is not a character string for logrotate, but is it impossible?
Change only one character from the String type string to uppercase
Memorandum No.4 "Get a character string and decorate it" [Java]
Try a similar search for Image Search using the Python SDK [Search]
Different from the import type of python. from A import B meaning
You who search and execute commands from a web browser 2
Extract character by character from UTF-8 string using standard multibyte function
Generate a unique collection of values from a collection that contains duplicate values
How to store a string from ArrayList to String in Java (Personal)
Finally, create a method for whether there is any character
Set the date and time from the character string with POI
Python> Read from a multi-line string instead of a file> io.StringIO ()
ABC --012-A & B & C
ABC --010 --A & B & C
Various character string operations
ABC --031 --A & B & C