matlab - Given two strings, how do I find number of reoccurences of one in another? -
for example, s1='abc'
, s2='kokoabckokabckoab'
. output should 3
. (number of times s1 appears in s2).
not allowed use for
or strfind
. can use reshape
,repmat
,size
.
i thought of reshaping s2, contain of possible strings of 3s:
s2 =
kok
oko
koa
oab
.... etc
but i'm having troubles here..
assuming have matrix reshaped format have in post, can replicate s1
, stack string such has many rows there in reshaped s2
matrix, equality operator. rows consist of 1s means have found match , search rows total sum equal total length of s1
. referring back post on dividing string overlapping substrings, can decompose string have posted in question so:
%// define s1 , s2 here s1 = 'abc'; len = length(s1); s2 = 'kokoabckokabckoab'; %// hankel starts here c = (1 : len).'; r = (len : length(s2)).'; nr = length(r); nc = length(c); x = [ c; r((2:nr)') ]; %-- build vector of user data cidx = (1:nc)'; ridx = 0:(nr-1); h = cidx(:,ones(nr,1)) + ridx(ones(nc,1),:); % hankel subscripts ind = x(h); % actual data %// end hankel script %// our data subseqs = s2(ind.'); %// case string length 1 if len == 1 subseqs = subseqs.'; end
subseqs
contains matrix of overlapping characters have alluded in post. you've noticed small bug if length of string 1, algorithm won't work. need make sure reshaped substring matrix consists of single column vector. if ran above code without checking length of s1
, row vector, , transpose result if case.
now, replicate s1
many times have rows in subseqs
of these strings stacked 2d matrix. after, equality operator.
eqs = subseqs == repmat(s1, size(subseqs,1), 1);
now, find column-wise sum , see elements equal length of string. produce single column vector 1
indicates have found match, , 0 otherwise:
sum(eqs, 2) == len ans = 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0
finally, add how many times substring matched, have add elements in vector:
out = sum(sum(eqs, 2) == len) out = 2
as such, have two instances abc
found in string.
Comments
Post a Comment