Topic:

Given a stringsand anon-emptystringp, find all the start indices ofp's anagrams ins.

Strings consists of lowercase English letters only and the length of both stringssandpwill not be larger than 20,100.

The order of output does not matter.

Example 1:

Input:
s: "cbaebabacd" p: "abc"

Output:
[0, 6]

Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".

Example 2:

Input:
s: "abab" p: "ab"

Output:
[0, 1, 2]

Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".

Answer:



Facing Problems:

  • template<class _Tp, class _Alloc> class std::vector' used without template parameters

->vector<int>::iterator it_i;

  • no match for 'operator=' (operand types are 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int>>}' and 'std::__cxx11::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char>>}')

->vector<string> vec_p; instead of using string p

  • expected unqualified-id before string constant

-> you need to inspect your semicolons and parentheses.

  • 'class std::vector<std::__cxx11::basic_string<char>>' has no member named 'pushback'; did you mean 'push_back'? ->It's intuitive in the question.

  • 'vec_s' was not declared in this scope -> The same.

  • Using operator++ is two results in case like for and while.

-> for(int i=0;i<3;i++) std::cout<<i

=> i=0,1,2

->while(i++<3) std::cout<<i

=> i=1,2,3

  • The index will become ASCII number when using char in the array.

-> for(auto c: char_arr) std::cout<<char_arr[c];

->ASCII number: 31(0)~3A(9), 41(A)~5A(Z) 61(a)~7A(Z), 20(space)

  • Reference:

Find All Anagrams in a String

vector::size

string::begin

results matching ""

    No results matching ""