Solution<\/strong><\/p>\nimport java.io.IOException;\n\nclass Result {\n public static int countingValleys(int steps, String path) {\n int valleyCount = 0, prePos, pos = 0;\n for (int i = 0; i < steps; i++) {\n prePos = pos;\n if (path.charAt(i) == 'D')\n pos--;\n else\n pos++;\n if (pos == 0 & prePos < 0)\n valleyCount++;\n }\n return valleyCount;\n }\n}\n\npublic class CountingValleys {\n public static void main(String[] args) throws IOException {\n int steps = 8;\n String path = \"UDDDUDUU\";\n int result = Result.countingValleys(steps, path);\n System.out.println(result);\n }\n}<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"Problem Solution<\/p>\n","protected":false},"author":1,"featured_media":694,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[43],"_links":{"self":[{"href":"https:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/posts\/728"}],"collection":[{"href":"https:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/comments?post=728"}],"version-history":[{"count":0,"href":"https:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/posts\/728\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/media\/694"}],"wp:attachment":[{"href":"https:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/media?parent=728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/categories?post=728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/tags?post=728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}