├── README.md ├── .idea ├── LeetCode.iml ├── encodings.xml ├── vcs.xml ├── modules.xml └── misc.xml ├── CMakeLists.txt ├── .gitignore ├── longest-absolute-file-path └── main.cpp ├── regular-expression-matching └── main.cpp ├── valid-number └── main.cpp └── max-points-on-a-line └── main.cpp /README.md: -------------------------------------------------------------------------------- 1 | Some problem i faced 2 | 😢😢 3 | -------------------------------------------------------------------------------- /.idea/LeetCode.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13) 2 | project(LeetCode) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(LeetCode-longest-absolute-file-path longest-absolute-file-path/main.cpp) 7 | add_executable(LeetCode-regular-expression-matching regular-expression-matching/main.cpp) 8 | add_executable(LeetCode-valid-number valid-number/main.cpp) 9 | add_executable(LeetCode-max-points-on-a-line max-points-on-a-line/main.cpp) 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 2 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 3 | 4 | # User-specific stuff 5 | .idea/**/workspace.xml 6 | .idea/**/tasks.xml 7 | .idea/**/usage.statistics.xml 8 | .idea/**/dictionaries 9 | .idea/**/shelf 10 | 11 | # Generated files 12 | .idea/**/contentModel.xml 13 | 14 | # Sensitive or high-churn files 15 | .idea/**/dataSources/ 16 | .idea/**/dataSources.ids 17 | .idea/**/dataSources.local.xml 18 | .idea/**/sqlDataSources.xml 19 | .idea/**/dynamic.xml 20 | .idea/**/uiDesigner.xml 21 | .idea/**/dbnavigator.xml 22 | 23 | # Gradle 24 | .idea/**/gradle.xml 25 | .idea/**/libraries 26 | 27 | # Gradle and Maven with auto-import 28 | # When using Gradle or Maven with auto-import, you should exclude module files, 29 | # since they will be recreated, and may cause churn. Uncomment if using 30 | # auto-import. 31 | # .idea/modules.xml 32 | # .idea/*.iml 33 | # .idea/modules 34 | 35 | # CMake 36 | cmake-build-*/ 37 | 38 | # Mongo Explorer plugin 39 | .idea/**/mongoSettings.xml 40 | 41 | # File-based project format 42 | *.iws 43 | 44 | # IntelliJ 45 | out/ 46 | 47 | # mpeltonen/sbt-idea plugin 48 | .idea_modules/ 49 | 50 | # JIRA plugin 51 | atlassian-ide-plugin.xml 52 | 53 | # Cursive Clojure plugin 54 | .idea/replstate.xml 55 | 56 | # Crashlytics plugin (for Android Studio and IntelliJ) 57 | com_crashlytics_export_strings.xml 58 | crashlytics.properties 59 | crashlytics-build.properties 60 | fabric.properties 61 | 62 | # Editor-based Rest Client 63 | .idea/httpRequests 64 | 65 | # Android studio 3.1+ serialized cache file 66 | .idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /longest-absolute-file-path/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Usman Zahid on 3/12/23. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class Solution 11 | { 12 | public: 13 | int lengthLongestPath(std::string input) 14 | { 15 | std::map counts; 16 | 17 | short level = 0; 18 | short maxLevel = 0; 19 | short currentNodeLength = 0; 20 | int max = 0; 21 | bool isFile = false; 22 | 23 | for (char &c : input) 24 | { 25 | bool isLast = &c == &input.back(); 26 | 27 | if (isLast) 28 | { 29 | currentNodeLength++; 30 | } 31 | 32 | if (c == '\n' || isLast) 33 | { 34 | int fullLength = currentNodeLength; 35 | 36 | if (level > 0) 37 | { 38 | auto p = counts[level - 1]; 39 | fullLength += p + 1; 40 | } 41 | 42 | for (short i = level; i <= maxLevel; ++i) 43 | { 44 | counts.erase(i); 45 | } 46 | 47 | counts[level] = fullLength; 48 | 49 | if (isFile && fullLength > max) 50 | { 51 | max = fullLength; 52 | } 53 | 54 | level = 0; 55 | currentNodeLength = 0; 56 | isFile = false; 57 | } 58 | else if (c == '\t') 59 | { 60 | level++; 61 | if (level > maxLevel) 62 | { 63 | maxLevel = level; 64 | } 65 | } 66 | else 67 | { 68 | currentNodeLength++; 69 | if (c == '.') 70 | { 71 | isFile = true; 72 | } 73 | } 74 | } 75 | 76 | return max; 77 | } 78 | }; 79 | 80 | int main() 81 | { 82 | auto result = Solution().lengthLongestPath( 83 | // "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" 84 | // "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" 85 | // "dir\n\t file.txt\n\tfile2.txt" 86 | "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"); 87 | 88 | std::cout << result; 89 | 90 | return 0; 91 | } -------------------------------------------------------------------------------- /regular-expression-matching/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Usman Zahid on 26/06/23. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | #define isMatchP(s, p) (s && p && (p == '.' || s == p)) 9 | 10 | class Solution { 11 | public: 12 | bool isMatch(std::string s, std::string p) { 13 | unsigned short sc = 0; 14 | unsigned short pc = 0; 15 | char starChar = false; 16 | char lastStarChar = false; 17 | 18 | if (p.empty()) { 19 | return s.empty(); 20 | } 21 | 22 | auto mpc = p.size() - 1; 23 | auto sEmpty = s.empty(); 24 | auto msc = sEmpty ? 0 : s.size() - 1; 25 | 26 | while (true) { 27 | char schar = (sc > msc || sEmpty) ? false : s[sc]; 28 | char cpchar = pc > mpc ? false : p[pc]; 29 | char pchar = starChar ? starChar : cpchar; 30 | 31 | if (!schar && !pchar) { 32 | return true; 33 | } 34 | 35 | if (!starChar && pc < mpc && p[pc + 1] == '*') { 36 | starChar = pchar; 37 | pc += 2; 38 | 39 | continue; 40 | } 41 | 42 | auto match = isMatchP(schar, pchar); 43 | 44 | if (starChar && match && isMatch(s.substr(sc), p.substr(pc))) { 45 | return true; 46 | } else if (match) { 47 | sc++; 48 | 49 | if (!starChar) { 50 | pc++; 51 | lastStarChar = false; 52 | } else { 53 | lastStarChar = pchar; 54 | } 55 | } else if (starChar || isMatchP(schar, lastStarChar)) { 56 | starChar = false; 57 | } else { 58 | return false; 59 | } 60 | } 61 | } 62 | }; 63 | 64 | #define assert(s, p, e) if(solution.isMatch(s, p) != e) { std::cout << #s << " match " #p << " should return " << e << "\n"; } else { std::cout << "ok\n"; } 65 | 66 | int main() { 67 | auto solution = Solution(); 68 | 69 | assert("aa", "a", false); 70 | assert("aaa", "aaaa", false); 71 | assert("aa", "a*", true); 72 | assert("aaa", "a*a", true); 73 | assert("a", "a*a", true); 74 | assert("ab", ".*", true); 75 | assert("aab", "c*a*b", true); 76 | assert("mississippi", "mis*is*ip*.", true); 77 | assert("ab", ".*c", false); 78 | assert("cccaaacaccabaab", "a*c*c*b*b*b*b*a*c", false); 79 | assert("aaa", "ab*a*c*a", true); 80 | assert("bbbba", ".*a*a", true); 81 | assert("ab", ".*..", true); 82 | assert("a", ".*..a", false); 83 | assert("a", ".*..a*", false); 84 | assert("", ".*", true); 85 | assert("bbba", ".*b", false); 86 | assert("abcdede", "ab.*de", true); 87 | assert("aasdfasdfasdfasdfas", "aasdf.*asdf.*asdf.*asdf.*s", true); 88 | assert("bccbbabcaccacbcacaa", ".*b.*c*.*.*.c*a*.c", false); 89 | assert("abbaaaabaabbcba", "a*.*ba.*c*..a*.a*.", true); 90 | 91 | std::cout << "Done !"; 92 | } 93 | -------------------------------------------------------------------------------- /valid-number/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Solution { 5 | public: 6 | void trim2(std::string &str) { 7 | std::string trim_chars = " "; 8 | std::string::size_type pos = str.find_last_not_of(trim_chars); 9 | if (pos != std::string::npos) { 10 | str.erase(pos + 1); 11 | pos = str.find_first_not_of(trim_chars); 12 | if (pos != std::string::npos) { 13 | str.erase(0, pos); 14 | } 15 | } else { 16 | str.erase(str.begin(), str.end()); 17 | } 18 | } 19 | 20 | bool isNumber(std::string s) { 21 | this->trim2(s); 22 | 23 | if (s.empty()) { 24 | return false; 25 | } 26 | 27 | bool hadN = false; 28 | bool hadE = false; 29 | bool hadDot = false; 30 | bool hadSign = false; 31 | 32 | for (auto c : s) { 33 | if (c == ' ') { 34 | return false; 35 | } else if (c == '-' || c == '+') { 36 | if (hadSign || hadN || hadDot) { 37 | return false; 38 | } 39 | 40 | hadSign = true; 41 | } else if (std::isdigit(c)) { 42 | hadN = true; 43 | } else if (c == '.') { 44 | if (hadDot || hadE) { 45 | return false; 46 | } 47 | 48 | hadDot = true; 49 | } else if (c == 'e') { 50 | if (hadE || !hadN) { 51 | return false; 52 | } 53 | 54 | hadN = false; 55 | hadSign = false; 56 | hadDot = false; 57 | hadE = true; 58 | } else { 59 | return false; 60 | } 61 | } 62 | 63 | char lastC = s.back(); 64 | if (lastC == 'e' || lastC == '-' || lastC == '+' || (!hadN && lastC == '.')) { 65 | return false; 66 | } 67 | 68 | return true; 69 | } 70 | }; 71 | 72 | #define assert(s, e) if(solution.isNumber(s) != e) { std::cout << #s << " should return " << e << "\n"; } else { std::cout << "ok\n"; } 73 | 74 | int main() { 75 | auto solution = Solution(); 76 | 77 | assert("0", true); 78 | assert("", false); 79 | assert(" ", false); 80 | assert(".", false); 81 | assert(" 0.1 ", true); 82 | assert("abc", false); 83 | assert("1 a", false); 84 | assert("2e10", true); 85 | assert(" -90e3 ", true); 86 | assert(" 1e", false); 87 | assert("e3", false); 88 | assert(" 6e-1", true); 89 | assert(" 99e2.5 ", false); 90 | assert("53.5e93", true); 91 | assert(" --6 ", false); 92 | assert("-+3", false); 93 | assert("95a54e53", false); 94 | assert("3.", true); 95 | assert(".-4", false); 96 | assert("+.8", true); 97 | 98 | std::cout << "Done !"; 99 | } 100 | -------------------------------------------------------------------------------- /max-points-on-a-line/main.cpp: -------------------------------------------------------------------------------- 1 | struct Point { 2 | int x; 3 | int y; 4 | 5 | Point() : x(0), y(0) {} 6 | 7 | Point(int a, int b) : x(a), y(b) {} 8 | }; 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | /** 18 | * Definition for a point. 19 | * struct Point { 20 | * int x; 21 | * int y; 22 | * Point() : x(0), y(0) {} 23 | * Point(int a, int b) : x(a), y(b) {} 24 | * }; 25 | */ 26 | class Solution { 27 | public: 28 | int maxPoints(std::vector points) { 29 | int n = points.size(); 30 | 31 | if(n <= 2) { 32 | return n; 33 | } 34 | 35 | auto max = 0; 36 | 37 | auto pairs = std::map, int>(); 38 | 39 | for (int i = 0; i < n; ++i) { 40 | auto verticals = 0; 41 | auto overlapping = 0; 42 | auto lmax = 0; 43 | 44 | auto p1 = points[i]; 45 | 46 | for (int j = i + 1; j < n; ++j) { 47 | auto p2 = points[j]; 48 | 49 | if(p2.x == p1.x) { 50 | if(p2.y == p1.y) { 51 | overlapping++; 52 | } else { 53 | verticals++; 54 | } 55 | } else { 56 | auto xd = p1.x - p2.x; 57 | auto yd = p1.y - p2.y; 58 | 59 | auto gcd = std::__gcd(xd, yd); 60 | 61 | xd /= gcd; 62 | yd /= gcd; 63 | 64 | auto p = std::pair(xd, yd); 65 | 66 | if(pairs.find(p) == pairs.end()) { 67 | pairs.emplace(p, 0); 68 | } 69 | 70 | pairs[p]++; 71 | 72 | } 73 | } 74 | 75 | for (auto const &[k, v] : pairs) { 76 | lmax = std::max(lmax, v); 77 | } 78 | 79 | lmax = std::max(lmax, verticals); 80 | max = std::max(max, lmax + overlapping + 1); 81 | 82 | pairs.clear(); 83 | } 84 | 85 | return max; 86 | } 87 | }; 88 | 89 | #define assert(e, ...) {auto r = solution.maxPoints(parse(__VA_ARGS__)); if(r != e) { std::cout << #__VA_ARGS__ << " expects " << #e << " got " << r << "\n"; } else { std::cout << "ok\n"; }} 90 | 91 | std::vector parse(std::vector> in) { 92 | auto points = std::vector(); 93 | 94 | for(auto p : in) { 95 | points.emplace_back(p[0], p[1]); 96 | } 97 | 98 | return points; 99 | } 100 | 101 | 102 | int main() { 103 | auto solution = Solution(); 104 | 105 | assert(3, {{1,1},{2,2},{3,3}}) 106 | assert(3, {{1,1},{1,1},{2,3}}) 107 | assert(4, {{1,1},{3,2},{5,3},{4,1},{2,3},{1,4}}) 108 | assert(6, {{84,250},{0,0},{1,0},{0,-70},{0,-70},{1,-1},{21,10},{42,90},{-42,-230}}) 109 | assert(3, {{4,0},{4,-1},{4,5}}) 110 | 111 | std::cout << "done"; 112 | } 113 | --------------------------------------------------------------------------------