fork download
  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <locale> // std::locale, std::tolower
  5. #include <algorithm>
  6.  
  7.  
  8. class Node
  9. {
  10. public:
  11. Node(std::string name, std::list<Node> children = {})
  12. {
  13. name_ = name;
  14. children_=children;
  15. };
  16.  
  17. static int cToLower(int c) {
  18. return tolower(c);
  19. }
  20.  
  21.  
  22.  
  23. int count_nodes_containing_string(std::string needle)
  24. {
  25. int count = 0;
  26. //TODO implement me, needs to be case-insensitive
  27. for ( Node nthChild : children_ )
  28. {
  29. //more children exist
  30. count += nthChild.count_nodes_containing_string(needle);
  31. }
  32.  
  33. //no more child
  34. std::string name_low = name_;
  35. std::transform(name_low.begin(), name_low.end(), name_low.begin(), ::tolower);
  36. std::transform(needle.begin(), needle.end(), needle.begin(), ::tolower);
  37. if (name_low.find(needle) != std::string::npos )
  38. {
  39. return count+1;
  40. }
  41. else
  42. {
  43. return count;
  44. }
  45.  
  46.  
  47. //needle; //TODO use the parameter
  48. //return -1;
  49. }
  50. private:
  51. std::string name_;
  52. std::list<Node> children_;
  53. };
  54.  
  55. #ifndef RunTests
  56. int main()
  57. {
  58. //Create an example tree
  59. Node n("root",{
  60. {"MagaZino",{
  61. {"I"},
  62. {"Love"},
  63. {"magazino"}
  64. }},
  65. {"Hello",{
  66. {"Hello",{
  67. {"Hello",{
  68. {"World"}
  69. }}
  70. }}
  71. }}
  72. });
  73. //Cout the solution
  74. std::cout << n.count_nodes_containing_string("test")<<std::endl;
  75. }
  76. #endif
Success #stdin #stdout 0.01s 5460KB
stdin
Standard input is empty
stdout
0