Skip to content

rnhc1000/leetcode

Repository files navigation

LeetCode - @Ricardo Ferreira

The only way to keep your game at a very high level is being challenged every day. Nice source of coding challenges at https://leetcode.com Let's dive into it!

Tools

  • Java 21
  • IntelliJ Community Edition
  • Gradle

Table of contents

Screenshot

Links

Built with

My Skills

What I practiced

public static boolean validAnagram(String s1, String s2) throws InterruptedException {
        logger.info("Starting Anagram.... - V2");
        startTimer();
        String regex = "\\s+";
        s1 = s1.replaceAll(regex, "").toLowerCase();
        s2 = s2.replaceAll(regex, "").toLowerCase();
        int lenStringOne = s1.length(), lenStringTwo = s2.length();
        logger.info(String.format("Length of each string provided as is: %d, %d", lenStringOne, lenStringTwo));
        if (lenStringOne != lenStringTwo) {
          logger.info("Its not feasible to have an anagram with the strings provided. Try again!");
          timeTaken();
          return false;
        }
        logger.info("Both strings are normalized..." + s1);
        char[] stringOne = s1.toCharArray(),stringTwo = s2.toCharArray();
        StringBuilder sbOne = new StringBuilder(), sbTwo = new StringBuilder();
        Thread thread = new Thread( () -> {
          Arrays.sort(stringOne);
          Arrays.sort(stringTwo);
          logger.info("Both strings are sorted...");
          for (char c : stringOne) {
            sbOne.append(c);
          }
          for (char c : stringTwo) {
            sbTwo.append(c);
          }
        });

        thread.start();
        thread.join();

        logger.info("Stopping watch...");
        timeTaken();
        stopWatchReset();
        return sbOne.toString().contentEquals(sbTwo.toString());
        }

Continued development

  • Next step: Threads, Concurrency, Parallelism. ExecutorService and CompletableFuture

Useful resources

Author

Acknowledgments

Releases

No releases published

Packages

No packages published

Languages