6
MATL, 13 bytes
yXf&-|wn<s1>s
Inputs are in reverse order: b, then a.
Try it online! Or verify all test cases.
Explanation
Consider inputs 'lol' and 'trololololol' as an example.
y % Implicit inputs: b, a. Duplicate second-top element in stack
% STACK: 'lol', 'trololololol', 'lol'
Xf % Find second string in first string. Produces a row ...
6
J, 35 33 31 24 bytes
1#.1<1#.#@[>|@-//~@I.@E.
Try it online!
-7 bytes after reading Luis Mendo's idea and realizing I could adapt it to J
I.@E. Indexes of matches
|@-//~@ Table of pairwise absolute differences
#@[> Is it less than the substring length? (produces 0-1 table)
1#. Sum rows
1< Greater than 1? (produces 0-1 list)
1#. Sum
6
JavaScript (ES6), 69 bytes
Expects (a)(b).
a=>g=(b,i=(t=0)-.1,p)=>~i?g(b,a.indexOf(b,i+1),i,q=b[i-p]?t-=~!q:0):t
Try it online!
How?
Given the last position i of b in a, we use a.indexOf(b, i + 1) to get the position of the next occurrence. We keep track of the previous position in p and figure out whether they overlap by testing if b[i - p] is ...
4
Python 3.8 (pre-release), 102 bytes
lambda a,b:sum(b==a[i-1:i+(T:=len(b)-1)]*(b in a[i+~T:i+T-1]or b in a[i:i+T*2])for
i in range(len(a)))
Try it online!
Explanation: it's pretty readable already. Just that i+~T == i-T-1, and the := is equivalent to an assignment but can be inserted in a lambda (to make the code shorter)
4
APL (Dyalog Unicode), 29 bytes (SBCS)
Mutually dependent lambdas
P←{≢⍸⍵=⎕CR'Q'}
Q←{≢⍸⍵=⎕CR'P'}
Try it online!
P←… establish a function with the following definition:
{…} dfn; the argument (a character) is ⍵:
⎕CR'…' Character Representation of the function called … (as a character matrix)
⍵= 2D mask indicating where the character is equal to elements of ...
4
Jelly, 15 bytes
wÐƤẹ1ạ€`<L}S>1S
A dyadic Link accepting a on the left and b on the right which yields the count.
Try it online! Or see the test-suite.
How?
wÐƤẹ1ạ€`<L}S>1S - Link: a, b
ÐƤ - for postfixes (of a):
w - first 1-indexed index (of b)
ẹ1 - indices of 1 (i.e. X = a list of starts of b in a)
...
4
PowerShell, 63 bytes
Inspired by @Wasif's awesome PowerShell answer
-6 bytes thanks to mazzy!
$P={$Q-replace"[^$args]"|% le*}
$Q={$P-replace"[^$args]"|% le*}
Try it online!
PowerShell, Independent Functions, 109 103 93 77 bytes
This version does not require the functions to be in the same file; they are completely independent of one ...
3
Jelly, 24 bytes
P
“ṾċḷḤ”Ṿċḷ“”Ḥ
Try it online!
Q
“ṾċḷḤ”Ṿċḷ”“Ḥ
Try it online!
P and Q are very similar, they both contain two of each of the same set of six characters. The only difference is “” vs ”“ (empty list vs a single open quote character).
How?
“ṾċḷḤ”Ṿċḷ..Ḥ - Link: character, C
“ṾċḷḤ” - list of characters = ['Ṿ', 'ċ', 'ḷ', 'Ḥ']
Ṿ - ...
3
Retina 0.8.2, 43 bytes
(?=(.+)(.*¶)\1$)(.(?!\2))+?((?=\1)|(?<=\1))
Try it online! Takes input on separate lines but link includes test suite that splits on comma for convenience. Explanation: The program consists of a single match stage that outputs the count of matches of the pattern within the string a. For each match:
(?=(.+)(.*¶)\1$)
The remainder ...
3
PowerShell, 128 122 bytes
function P($x){cd Function:;(gc Q)-replace"[^$($x)]"|% le*}
function Q($x){(gc $psCommandPath)[0]-replace"[^$($x)]"|% le*}
Try it online!
-6 bytes Thanks to mazzy and Zaelin Goodman
Two functions work in different ways.
P() will read Q()'s source code and count occurrences of the given character.
Q() will read ...
3
R, 90 89 bytes
function(x,y,z=rle(diff(el(gregexpr(paste0("(?=",y,")"),x,,T)))<nchar(y)))sum(z$l[z$v]+1)
A byte saved by Dominic van Essen.
Try it online!
2
Charcoal, 22 bytes
I↨E⌕Aθη⊙↔⁻⌕Aθηι∧λ‹λLη¹
Try it online! Link is to verbose version of code. Explanation: Conveniently, Charcoal's FindAll command also includes overlapping matches.
θ First input
η Second input
⌕A Find all matches
E Map over positions
⌕Aθη ...
2
Perl 5, 117 bytes
sub f{($a,$b,$f,%s,$c)=@_;!$s{$x=1+index$a,$b,$_}++&&$x&&($w=$f&&$x-$f<length$b,$f=$x,$c+=$j*$w,$j=2-$w)for 0..99;$c}
Try it online!
2
R, version-dependent, 110 84 bytes
Edit: -26 bytes with inspiration from user81655's answer
Note: Function-pairs #1 and #2 below depend on the version-dependent output formatting of R functions, and work on my locally-installed R version 3.2.1, but unfortunately not on version 3.5.2 which is installed on TIO. A TIO-compliant (but longer) adjusted version of ...
2
JavaScript (V8), 51 bytes
p=(k,s='q='+q)=>s.split(k).length-1
q=k=>p(k,'p='+p)
Try it online!
Works assuming we're allowed to read the other program (grey area but not explicitly disallowed in the rules).
1
Python 3, 106 100 96 bytes
Saved 10 bytes thanks to Dominic van Essen!!!
import inspect
f=lambda c:inspect.getsource(g).count(c)
g=lambda c:inspect.getsource(f).count(c)
Try it online!
Two mutually dependent lambdas that simply read each other's source code and return the input character count.
1
Zsh, 104 96 56 bytes
a=`<$0` b=$1;<<<${#a//[^$b]}
b=`<$0` a=$1;<<<${#b//[^$a]}
Try it online!
Try it* online!
Try it online!
Both programs are independent (they don't read each other's source). I found the best way to tackle the challenge was to make two self-counting programs with the same character counts. Using assignment to ...
1
Japt, 12 bytes
Been quite a while since I've done any Japt, probably has some room for improvement.
ðV
äÏ-X<Vl
è
Try it out here.
Explanation:
ðV # Get all indices of needle in input
äÏ-X<Vl # For every consecutive pair in the result, check if their distance is small enough that they overlap
è # Return the number of items that were true
1
Perl 5 (-00p), 54 bytes
$_=/
(.+)(.*\1)$/?()=/(?=$1$2$2)|(?<=$1)$2$2(?!$2)/g:0
Try it online!
1
APL(Dyalog Unicode), 20 bytes SBCS
+/1<1⊥≢⍤⊣>∘|∘.-⍨⍤⍸⍤⍷
Try it on APLgolf!
A train submission which takes b on the left, and a on the right.
-2 bytes from Adám(helped trainify the function)
Explanation
⍷ boolean array where b occurs in a
⍸⍤ indices of 1s in it
∘.-⍨⍤ all pairwise differences
...
1
05AB1E, 18 bytes
.sIÅ?ƶ0KDδαIg‹O1›O
Port of @JonathanAllan's Jelly answer.
Try it online or verify all test cases.
Explanation:
.s # Get all suffices of the (implicit) input-string
IÅ? # Check for each if they start with the second input-string
# (1 if truthy; 0 if falsey)
ƶ # Multiply ...
1
Rust, 44 bytes
|a|a.iter().rev().take_while(|&&i|i).count()
Try it online!
explanation:
|a|a.iter().rev().take_while(|&&i|i).count() //anonymous function
|a|a.iter() //iterate over the input
.rev() //reverse the iterator
.take_while(|&&i|i) ...
1
Husk, 11 bytes
!¡(dṁ§eL←gd
Try it online!
! # get the arg2-th element of
¡ # the infinite list by repeatedly applying
# (starting with arg1):
(d # get the digits of
ṁ # applying to each of
gd # the groups of identical neighbouring digits:
§eL← # combine length + first ...
1
Stax, 3 bytes
VIm
Run and debug it
the other Stax answer is invalid since it starts from zero. This is shorter, anyway.
1
v³, 3 bytes
+.=
Unwrapped:
+
. = . .
.
Simple explanation:
+ Add one to memory cell (starts at zero)
= Print memory cell as number
. No-op
The instruction pointer starts at '+' then "falls" into '=', then the '.' at the bottom.
The IP then falls to the '.' on the far right, as if the code is mapped onto a cube.
The IP then falls to '+',...
Only top voted, non community-wiki answers of a minimum length are eligible
Related Tags
counting × 112code-golf × 105
string × 27
number × 15
grid × 11
array-manipulation × 10
ascii-art × 9
sequence × 8
math × 7
combinatorics × 7
code-challenge × 4
geometry × 4
restricted-source × 3
primes × 3
binary × 3
base-conversion × 3
image-processing × 3
subsequence × 3
arithmetic × 2
popularity-contest × 2
game × 2
date × 2
matrix × 2
permutations × 2
path-finding × 2