Find the longest consecutive number string

Source: Internet
Author: User

Input Description:

Enter a string.

Output Description:

The longest numeric string in the output string and its length.

Input Example:
abcd12345ed125ss123058789, If there is a repetition of the maximum length of the number string, then output together, such as 1456jk4789, output 14564789,4
Output Example:
123058789,9


Idea: First the string of numbers out, and then take out the maximum length of the digital string and output, just at the beginning I use HashMap key to store a digital string, the value of the string length, considering the HashMap storage disorder, it is changed to Linkedhashmap, Then, according to the value of map, the value of the largest map element and output, but ignore a special case, that is, when the maximum substring has several and the same, the map because key can not be duplicated, will only store one, it is defective, but is AC, then use list to store, Resolves this problem and is AC,

The defective code is first:
1 Importjava.util.ArrayList;2 Importjava.util.Collections;3 ImportJava.util.Comparator;4 ImportJava.util.Iterator;5 ImportJava.util.LinkedHashMap;6 Importjava.util.List;7 ImportJava.util.Map;8 ImportJava.util.Map.Entry;9 ImportJava.util.Scanner;Ten ImportJava.util.Set; One  A  Public classTest10 { -  -      Public Static voidMain (string[] args) { theScanner sc =NewScanner (system.in); -          while(Sc.hasnext ()) { -String s =Sc.next (); -Map<string,integer> MP =NewLinkedhashmap<string,integer> ();//guaranteeing the ordering of elements using the linked hash +String S1 = S.replaceall ("[A-Z]", "");//Replace all letters in a string with spaces -string[] str = s1.split ("");//split a string with a space +              for(inti = 0; i < str.length; i++) {//The string is stored in a sequence of numbers in a map, where key is a number string and value is the string length A                 if(Str[i].length ()! = 0) {//because there are continuous spaces, the use of space to split a plurality of empty strings, the blank string is not considered, so there is this judgment at                     if(Str[i].charat (0)! = ") { - Mp.put (Str[i], str[i].length ()); -                     } -                 } -             } - //System.out.println (MP); in //System.out.println ("-------"); -  tolist<map.entry<string, integer>> li =NewArraylist<map.entry<string,integer>> ();//used to store map.entry<string, integer> type +set<entry<string, Integer>>se = Mp.entryset ();//get Map of map.entry<string, integer> type -Iterator<entry<string, integer>> it =se.iterator (); the              while(It.hasnext ()) {//put map.entry<string, integer> type elements in list to sort *entry<string, integer> en =It.next (); $ li.add (en);Panax Notoginseng //System.out.println (en); -             } the //System.out.println ("-------"); +Collections.sort (Li,NewComparator<entry<string, integer>> () {//Use the Collections Collection tool class to sort the entry type elements in a list in natural ascending order by value A  the @Override +                  Public intCompare (Entry<string, integer>O1, -Entry<string, integer>O2) { $                     //TODO auto-generated Method Stub $                     returnO1.getvalue (). CompareTo (O2.getvalue ()); -                 } -             }); the              - //iterator<entry<string, integer>> it1 = Se.iterator (); Wuyi //While (It1.hasnext ()) { the //System.out.println (It1.next ()); - //            } Wu //System.out.println ("-------"); -  About  $StringBuffer SB =NewStringBuffer ();//used to store a string of the maximum length and equal number -              for(intI=0;i<li.size (); i++){ -                 if(Li.get (Li.size ()-1). GetValue () = =Li.get (i). GetValue ()) { - sb.append (string.valueof (Li.get (i). GetKey ())); A                 } +             } theSystem.out.println (sb+ "," +li.get (Li.size ()-1). GetValue ()); -         } $     } the}

The above code if input 1234hj1234, will not output the correct result 12341234,4, output 1234, 4, the following use list

1 Importjava.util.ArrayList;2 Importjava.util.Collections;3 ImportJava.util.Comparator;4 Importjava.util.List;5 ImportJava.util.Scanner;6 7  Public classTest11 {8 9      Public Static voidMain (string[] args) {TenScanner sc =NewScanner (system.in); One          while(Sc.hasnext ()) { AString s =Sc.next (); -List<string> Li =NewArraylist<string>(); -String S1 = S.replaceall ("[A-Z]", "");//Replace all letters in a string with spaces thestring[] str = s1.split ("");//split a string with a space -              for(inti = 0; i < str.length; i++) {//The string is stored in a sequence of numbers in a map, where key is a number string and value is the string length -                 if(Str[i].length ()! = 0) {//because there are continuous spaces, the use of space to split a plurality of empty strings, the blank string is not considered, so there is this judgment -                     if(Str[i].charat (0)! = ") { + Li.add (Str[i]); -                     } +                 } A             } at  -Collections.sort (Li,NewComparator<string>() { -  - @Override -                  Public intCompare (String O1, string O2) {//This is a natural sort based on the length of the string element in the list. -                     //TODO auto-generated Method Stub in                     returninteger.valueof (O1.length ()). CompareTo ( - integer.valueof (O2.length ())); to                 } +             }); -StringBuffer SB =NewStringBuffer ();//used to store a string of the maximum length and equal number the              for(inti = 0; I < li.size (); i++) { *                 if(Li.get (i). Length () = = Li.get (Li.size ()-1). Length ()) { $ Sb.append (Li.get (i));Panax Notoginseng                 } -             } the  +SYSTEM.OUT.PRINTLN (SB + "," + Li.get (Li.size ()-1). Length ()); A         } the     } +}


Find the longest consecutive number string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.