{"id":17703,"date":"2022-10-26T17:13:32","date_gmt":"2022-10-26T11:43:32","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/"},"modified":"2022-10-26T17:13:32","modified_gmt":"2022-10-26T11:43:32","slug":"solved-alphanumeric-increment-algorithm-in-java-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/","title":{"rendered":"[Solved] Alphanumeric increment algorithm in JAVA [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-38210171\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"38210171\" data-parentid=\"38209985\" data-score=\"8\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Here&#8217;s 3 solutions: the first two are somewhat arithmetic incrementations while the third is more a character manipulations.<\/p>\n<p>The 3 implementations all pass the same unit tests:<\/p>\n<pre><code> assertEquals(\"1DDA01A\", MyClass.increment(\"1DDA00Z\"));\n assertEquals(\"1A9AV00\", MyClass.increment(\"1A9AU99\"));\n assertEquals(\"AFH00\", MyClass.increment(\"AFG99\"));\n assertEquals(\"A2GF24\", MyClass.increment(\"A2GF23\"));\n assertEquals(\"ABAA0000\", MyClass.increment(\"AAZZ9999\"));\n assertEquals(\"11AB0A\", MyClass.increment(\"11AA9Z\"));\n<\/code><\/pre>\n<p><strong>First:<\/strong><\/p>\n<pre><code>public static String increment(String number) {\n    Pattern compile = Pattern.compile(\"^(.*?)([9Z]*)$\");\n    Matcher matcher = compile.matcher(number);\n    String left=\"\";\n    String right=\"\";\n    if(matcher.matches()){\n         left = matcher.group(1);\n         right = matcher.group(2);\n    }\n    number = !left.isEmpty() ? Long.toString(Long.parseLong(left, 36) + 1,36):\"\";\n    number += right.replace(\"Z\", \"A\").replace(\"9\", \"0\");\n    return number.toUpperCase();\n}\n<\/code><\/pre>\n<p><strong>Second:<\/strong><\/p>\n<pre><code>public static String increment(String number) {\n    Pattern compile = Pattern.compile(\"^(.*?)([0-9]*|[A-Z]*)$\");\n    Matcher matcher = compile.matcher(number);\n    String remaining = number;\n    String currentGroup = \"\";\n    String result = \"\";\n\n    boolean continueToNext = true;\n        while (matcher.matches() &amp;&amp; continueToNext) {\n        remaining = matcher.group(1);\n        currentGroup = matcher.group(2);\n        int currentGroupLength = currentGroup.length();\n        int base = currentGroup.matches(\"[0-9]*\") ? 10 : 36;\n        currentGroup = Long.toString(Long.parseLong(\"1\" + currentGroup, base) + 1, base);  \/\/ The \"1\" if just to ensure that \"000\" doesn't become 0 (and thus losing the original string length)\n        currentGroup = currentGroup.substring(currentGroup.length() - currentGroupLength, currentGroup.length());\n        continueToNext = Long.valueOf(currentGroup, base) == 0;\n        if (base == 36) {\n            currentGroup = currentGroup.replace(\"0\", \"A\");\n        }\n\n        result = currentGroup + result;\n        matcher = compile.matcher(remaining);\n    }\n\n    result = remaining + result;\n    return result.toUpperCase();\n}\n<\/code><\/pre>\n<p><strong>Third<\/strong> :<\/p>\n<p>This works with your current &#8220;reqs&#8221;. Compared to how the question what asked at the beginning, this is not just a &#8220;left-part composed of letter&#8221; + &#8220;right part composed of digits&#8221;. Now, it&#8217;s &#8220;anything goes&#8221;, and letters roll from A to Z to A, while digits from 0 to 9 to 0. When a letter reaches Z, it is reset to A, then the digit\/letter on its left is incremented.<\/p>\n<p>If all numbers are incremented, it does not add a new digit on the left. You did not mention that in your question, but I&#8217;m sure you can figure this out from here:<\/p>\n<pre><code>public static String increment(String number) {\n    char[] cars = number.toUpperCase().toCharArray();\n    for (int i = cars.length - 1; i &gt;= 0; i--) {\n        if (cars[i] == 'Z') {\n            cars[i] = 'A';\n        } else if (cars[i] == '9') {\n            cars[i] = '0';\n        } else {\n            cars[i]++;\n            break;\n        }\n    }\n    return String.valueOf(cars);\n}\n<\/code><\/pre>\n<p>As for the &#8220;count&#8221;, your example isn&#8217;t sufficient to grasp the logic. Does it count only numbers ? what about the letters ? Does it follow a baseXx ?<\/p>\n<p>how can AA010-AAA003 = 7, the 3 A&#8217;s versus 2 A&#8217;s do no matter ?<br \/>\nI feel this is rather on you to understand what are your requirements (ie: homework..)<\/p>\n<p>Technically, this answers the question as it was asked originally (with many modifications along the way).<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">30<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Alphanumeric increment algorithm in JAVA [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Here&#8217;s 3 solutions: the first two are somewhat arithmetic incrementations while the third is more a character manipulations. The 3 implementations all pass the same unit tests: assertEquals(&#8220;1DDA01A&#8221;, MyClass.increment(&#8220;1DDA00Z&#8221;)); assertEquals(&#8220;1A9AV00&#8221;, MyClass.increment(&#8220;1A9AU99&#8221;)); assertEquals(&#8220;AFH00&#8221;, MyClass.increment(&#8220;AFG99&#8221;)); assertEquals(&#8220;A2GF24&#8221;, MyClass.increment(&#8220;A2GF23&#8221;)); assertEquals(&#8220;ABAA0000&#8221;, MyClass.increment(&#8220;AAZZ9999&#8221;)); assertEquals(&#8220;11AB0A&#8221;, MyClass.increment(&#8220;11AA9Z&#8221;)); First: public static String increment(String number) { Pattern compile = Pattern.compile(&#8220;^(.*?)([9Z]*)$&#8221;); Matcher matcher = compile.matcher(number); &#8230; <a title=\"[Solved] Alphanumeric increment algorithm in JAVA [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/\" aria-label=\"More on [Solved] Alphanumeric increment algorithm in JAVA [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[457,2163,900,323],"class_list":["post-17703","post","type-post","status-publish","format-standard","hentry","category-solved","tag-algorithm","tag-alphanumeric","tag-increment","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Alphanumeric increment algorithm in JAVA [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Alphanumeric increment algorithm in JAVA [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Here&#8217;s 3 solutions: the first two are somewhat arithmetic incrementations while the third is more a character manipulations. The 3 implementations all pass the same unit tests: assertEquals(&quot;1DDA01A&quot;, MyClass.increment(&quot;1DDA00Z&quot;)); assertEquals(&quot;1A9AV00&quot;, MyClass.increment(&quot;1A9AU99&quot;)); assertEquals(&quot;AFH00&quot;, MyClass.increment(&quot;AFG99&quot;)); assertEquals(&quot;A2GF24&quot;, MyClass.increment(&quot;A2GF23&quot;)); assertEquals(&quot;ABAA0000&quot;, MyClass.increment(&quot;AAZZ9999&quot;)); assertEquals(&quot;11AB0A&quot;, MyClass.increment(&quot;11AA9Z&quot;)); First: public static String increment(String number) { Pattern compile = Pattern.compile(&quot;^(.*?)([9Z]*)$&quot;); Matcher matcher = compile.matcher(number); ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-26T11:43:32+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Alphanumeric increment algorithm in JAVA [closed]\",\"datePublished\":\"2022-10-26T11:43:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/\"},\"wordCount\":218,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"algorithm\",\"alphanumeric\",\"increment\",\"java\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/\",\"name\":\"[Solved] Alphanumeric increment algorithm in JAVA [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-26T11:43:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Alphanumeric increment algorithm in JAVA [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Alphanumeric increment algorithm in JAVA [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Alphanumeric increment algorithm in JAVA [closed] - JassWeb","og_description":"[ad_1] Here&#8217;s 3 solutions: the first two are somewhat arithmetic incrementations while the third is more a character manipulations. The 3 implementations all pass the same unit tests: assertEquals(\"1DDA01A\", MyClass.increment(\"1DDA00Z\")); assertEquals(\"1A9AV00\", MyClass.increment(\"1A9AU99\")); assertEquals(\"AFH00\", MyClass.increment(\"AFG99\")); assertEquals(\"A2GF24\", MyClass.increment(\"A2GF23\")); assertEquals(\"ABAA0000\", MyClass.increment(\"AAZZ9999\")); assertEquals(\"11AB0A\", MyClass.increment(\"11AA9Z\")); First: public static String increment(String number) { Pattern compile = Pattern.compile(\"^(.*?)([9Z]*)$\"); Matcher matcher = compile.matcher(number); ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-26T11:43:32+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Alphanumeric increment algorithm in JAVA [closed]","datePublished":"2022-10-26T11:43:32+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/"},"wordCount":218,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["algorithm","alphanumeric","increment","java"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/","name":"[Solved] Alphanumeric increment algorithm in JAVA [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-26T11:43:32+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-alphanumeric-increment-algorithm-in-java-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Alphanumeric increment algorithm in JAVA [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=17703"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17703\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=17703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=17703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=17703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}