leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit ab4fef867852ac6485004582246c8a653dd58862
parent d9c665c3d6886216c99798d8c52099ff781b5a71
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sun,  8 Jan 2023 00:27:36 +0100

Shell problems

- format.sh
  * format only cpp files, ignore others

Diffstat:
AProblems/0192.sh | 5+++++
AProblems/0193.sh | 1+
AProblems/0194.sh | 5+++++
AProblems/0195.sh | 1+
MREADME.md | 4++++
Mformat.sh | 2+-
6 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/Problems/0192.sh b/Problems/0192.sh @@ -0,0 +1,5 @@ +// original +for word in $(tr ' ' '\n' < words.txt | sort | uniq); do grep -o "\<$word\>" words.txt | wc -l | xargs echo $word; done | sort -k2 -nr + +// using uniq -c instead of counting word by word +cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{ print $2, $1 }' diff --git a/Problems/0193.sh b/Problems/0193.sh @@ -0,0 +1 @@ +grep - E '^(([0-9]{3}-)|(\([0-9]{3}\) ))[0-9]{3}-[0-9]{4}$' file.txt diff --git a/Problems/0194.sh b/Problems/0194.sh @@ -0,0 +1,5 @@ +// original +for i in $(head -n1 file.txt | wc -w | xargs seq 1); do cut -d ' ' -f$i file.txt | xargs; done + +// same idea, little bit more elegant +head -n1 file.txt | wc -w | xargs seq 1 | xargs -n1 -i{} sh -c "cut -d' ' -f{} file.txt | xargs" diff --git a/Problems/0195.sh b/Problems/0195.sh @@ -0,0 +1 @@ +sed - n '10,+p' file.txt diff --git a/README.md b/README.md @@ -90,6 +90,10 @@ for solving problems. | 0167 | Medium | [Two Sum II - Input Array Is Sorted](Problems/0167.cpp) | | 0173 | Medium | [Binary Search Tree Iterator](Problems/0173.cpp) | | 0189 | Medium | [Rotate Array](Problems/0189.cpp) | +| 0192 | Medium | [Word Frequency](Problems/0192.sh) | +| 0193 | Easy | [Valid Phone Numbers](Problems/0193.sh) | +| 0194 | Medium | [Transpose File](Problems/0194.sh) | +| 0195 | Easy | [Tenth Line](Problems/0195.sh) | | 0198 | Medium | [House Robber](Problems/0198.cpp) | | 0199 | Medium | [Binary Tree Right Side View](Problems/0199.cpp) | | 0200 | Medium | [Number of Islands](Problems/0200.cpp) | diff --git a/format.sh b/format.sh @@ -1,7 +1,7 @@ #!/bin/bash # Format newly added files using clang-format -for i in $(git status | grep -Eo "(Problems|Templates).*") +for i in $(git status | grep -Eo "(Problems|Templates).*\.cpp") do echo "Formating: $i" clang-format -i $i