Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // you can use includes, for example:
- #include <algorithm>
- #include <string>
- #include <vector>
- // 1. iterate on range A to B
- // 2. convert int to string
- // 3. count occurences of lovely numbers
- // 4. return number
- int solution(int A, int B)
- {
- int cnt;
- std::string num_string;
- int lovely_cnt = 0;
- bool is_lovely;
- // iterates on range [A, B]
- for(int i = A ; i <= B ; i++)
- {
- bool is_lovely = true;
- cnt = 0;
- num_string = std::to_string(i);
- // must be lovely
- if(num_string.length() < 3)
- {
- lovely_cnt++;
- continue;
- }
- // checking number's loveliness
- for(int j = 0 ; j < num_string.length() ; j++)
- {
- cnt = std::count(num_string.begin(), num_string.end(), num_string[j]);
- if(cnt >= 3)
- {
- is_lovely = false;
- break;
- }
- }
- if(is_lovely)
- {
- lovely_cnt++;
- }
- }
- return lovely_cnt;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement