fork download
  1. struct TrieNode {
  2. TrieNode* child[26];
  3. int idx;
  4. vector<int>pal;
  5.  
  6. TrieNode() {
  7. for (int i = 0; i<26; i++) {
  8. child[i] = nullptr;
  9. }
  10. idx = -1;
  11. }
  12. };
  13.  
  14. class Trie {
  15. private:
  16. TrieNode* par;
  17. bool check(string &t, int i) {
  18. int n = t.size();
  19. int st = i;
  20. int end = n - 1;
  21. while (st <= end) {
  22. if (t[st] != t[end]) {
  23. return false;
  24. }
  25. st++;
  26. end--;
  27. }
  28. return true;
  29. }
  30.  
  31. public:
  32. Trie() {
  33. par = new TrieNode();
  34. }
  35.  
  36. void insert(string &s, int ind) {
  37. int n = s.size();
  38. TrieNode* node = par;
  39. for (int i = 0; i<n; i++) {
  40. int ch = s[i]-'a';
  41. if (check(s, i)) {
  42. node->pal.push_back(ind);
  43. }
  44. if (node->child[ch] == nullptr) {
  45. node->child[ch] = new TrieNode();
  46. }
  47. node = node->child[ch];
  48. }
  49. node->idx = ind;
  50. node->pal.push_back(ind);
  51. }
  52.  
  53. bool isValid(string &s, int ind) {
  54. int n = s.size();
  55. TrieNode*node = par;
  56.  
  57. for (int i = 0; i<n; i++) {
  58. // if taht end is not presnet
  59. // if there is end ther the we can check for the remaining sequence is tahta pal
  60. int ch = s[i]-'a';
  61. if (node->idx != -1 && check(s, i) && node->idx != ind) {
  62. return true;
  63. }
  64. if (node->child[ch] == nullptr) {
  65. return false;
  66. }
  67. node = node->child[ch];
  68.  
  69. }
  70. for (auto &x:node->pal) {
  71. if (x != ind)return true;
  72. }
  73. return false;
  74. }
  75. };
  76. class Solution {
  77. public:
  78. bool palindromePair(vector<string>& arr) {
  79. // Code here
  80. int n = arr.size();
  81. if (n <= 1)return false;
  82. // we have top find atleat one pair where
  83. // if oi joined that two it becomes palindrome
  84. // size wont matter
  85. // we can use trie i guess
  86. // first we have to insert all thekement sinto the trie
  87. // na an first we haver to reverse the word and check if that is prenst or not
  88. // ansd if there eneds a word means it is true//otherwise we have to check
  89. // s o we have to push the smaller elemsnts
  90. // so that when it fully compares means remaing we can check if remaing sequence is a p
  91. // if tahasts a pal return true;
  92.  
  93. Trie trie;
  94.  
  95. for (int i = 0; i<n; i++) {
  96.  
  97. trie.insert(arr[i], i);
  98. }
  99.  
  100. for (int i = 0; i<n; i++) {
  101. string t = arr[i];
  102. reverse(t.begin(), t.end());
  103. if (trie.isValid(t, i)) {
  104. return true;
  105. }
  106. }
  107. return false;
  108.  
  109. }
  110. };
  111.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:2: error: ‘vector’ does not name a type
  vector<int>pal;
  ^~~~~~
prog.cpp:17:13: error: ‘string’ has not been declared
  bool check(string &t, int i) {
             ^~~~~~
prog.cpp:36:14: error: ‘string’ has not been declared
  void insert(string &s, int ind) {
              ^~~~~~
prog.cpp:53:15: error: ‘string’ has not been declared
  bool isValid(string &s, int ind) {
               ^~~~~~
prog.cpp: In member function ‘bool Trie::check(int&, int)’:
prog.cpp:18:13: error: request for member ‘size’ in ‘t’, which is of non-class type ‘int’
   int n = t.size();
             ^~~~
prog.cpp:22:12: error: invalid types ‘int[int]’ for array subscript
    if (t[st] != t[end]) {
            ^
prog.cpp:22:22: error: invalid types ‘int[int]’ for array subscript
    if (t[st] != t[end]) {
                      ^
prog.cpp: In member function ‘void Trie::insert(int&, int)’:
prog.cpp:37:13: error: request for member ‘size’ in ‘s’, which is of non-class type ‘int’
   int n = s.size();
             ^~~~
prog.cpp:40:16: error: invalid types ‘int[int]’ for array subscript
    int ch = s[i]-'a';
                ^
prog.cpp:42:11: error: ‘struct TrieNode’ has no member named ‘pal’
     node->pal.push_back(ind);
           ^~~
prog.cpp:50:9: error: ‘struct TrieNode’ has no member named ‘pal’
   node->pal.push_back(ind);
         ^~~
prog.cpp: In member function ‘bool Trie::isValid(int&, int)’:
prog.cpp:54:13: error: request for member ‘size’ in ‘s’, which is of non-class type ‘int’
   int n = s.size();
             ^~~~
prog.cpp:60:16: error: invalid types ‘int[int]’ for array subscript
    int ch = s[i]-'a';
                ^
prog.cpp:70:22: error: ‘struct TrieNode’ has no member named ‘pal’
   for (auto &x:node->pal) {
                      ^~~
prog.cpp: At global scope:
prog.cpp:78:22: error: ‘vector’ has not been declared
  bool palindromePair(vector<string>& arr) {
                      ^~~~~~
prog.cpp:78:28: error: expected ‘,’ or ‘...’ before ‘<’ token
  bool palindromePair(vector<string>& arr) {
                            ^
prog.cpp: In member function ‘bool Solution::palindromePair(int)’:
prog.cpp:80:11: error: ‘arr’ was not declared in this scope
   int n = arr.size();
           ^~~
prog.cpp:101:4: error: ‘string’ was not declared in this scope
    string t = arr[i];
    ^~~~~~
prog.cpp:101:4: note: suggested alternative: ‘trie’
    string t = arr[i];
    ^~~~~~
    trie
prog.cpp:102:12: error: ‘t’ was not declared in this scope
    reverse(t.begin(), t.end());
            ^
prog.cpp:102:4: error: ‘reverse’ was not declared in this scope
    reverse(t.begin(), t.end());
    ^~~~~~~
stdout
Standard output is empty