How to return a long sequence of characters in a string in java?

Here is what I am doing, but I have not found the right answer.

Example. If I have the sequence "hellloo", the output will be "lll". Please tell me what's wrong?

public class LongestSequenceOfChar {
    static String testcase1="hellloo";

    public static void main(String[] args) {
        LongestSequenceOfChar test = new LongestSequenceOfChar();
        String result = test.longestSequenceOfChar(testcase1);
        System.out.println(result);
    }
    public String longestSequenceOfChar(String str){
        String result="";
        for(int i=0;i<str.length();i++){
            char ch=str.charAt(i);
            for(int j=i+1;j<str.length();j++){
                char ch1=str.charAt(j);
                if(ch!=ch1){
                    continue;
                }
                result+=ch;
            }
        }
        return result;
    }
}
+4
source share
6 answers

If there are three "l", you add only two, and in the next step - two "l", and you add one of them. Then the same thing with two "o", where you add one. You only need to clear the result line when you go to the next letter and before saving the result in another variable, but only if it is longer!

public String longestSequenceOfChar(String str){
    String interimresult="";
    String result="";              //final result
    for(int i=0;i<str.length();i++){
        char ch=str.charAt(i);
        interimresult += ch;       //add the letter once
        for(int j=i+1;j<str.length();j++){
            char ch1=str.charAt(j);
            if(ch!=ch1){
                break;
            }
            interimresult +=ch;
        }
        if(interimresult.length()>result.length())//store the result if it is longer 
            result = interimresult;
        interimresult = "";                   //clear to continue with the next letter
    }
    return result;
}
+2
source

, . , reset result .

:

  • 26 ( ). char 1 .
  • HashMap, char , . char, 0- , , .

. , .

+5
1. Create a HashMap<Character,Integer>.. Integer-->count
2. Start from the beginning of your String.. For each character, check if it is already present in the hashmap
  a. If Yes, just increment the count
  b. if No, then add the character as key to the Map and set its count value to 1. 
+4

:

public String longestSequenceOfChar(String str) {
    String result = "";

    for (int i = 0; i < str.length(); i++) {
        int j = i;
        while(j < str.length() && str.charAt(j) == str.charAt(i)) {
            j++;
        }

        // If this one is longer than previous, then asign it to result.
        if(j - i > result.length()) {
            result = str.substring(i, j);
        }
    }
    return result;
}
+2

This can be easily resolved using the HashMap. Check out this sample code:

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MaximumOccuringCharUsingHashMap {
  public static void main(String[] args) {
  String test = "test samples";
  MaximumOccuringCharUsingHashMap mc = 
      new MaximumOccuringCharUsingHashMap();
  System.out.println( mc.findMaximunOccurenceCharacter(test));
}
  char findMaximunOccurenceCharacter(String input){
      Map<Character, Integer> countHash = 
          new HashMap<Character, Integer>();
      for(int i=0; i<input.length() ;i++ ){
          char currentChar = input.charAt(i);
          if(countHash.get(currentChar)==null){
              countHash.put(currentChar, 1);
          }else{
              countHash.
              put(currentChar, countHash.get(currentChar)+1);
          }
      }

      int max = Collections.max(countHash.values());

      char maxCharacter =0;
      for(Entry<Character, Integer> entry :countHash.entrySet()){
          if(entry.getValue() == max){
              maxCharacter = entry.getKey();
          }
      }
      return maxCharacter;
  }
}

Above, the code will print s as an output that meets the maximum number of times in a given line.

+1
source

Try it...

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println(maxLen(null));
        System.out.println(maxLen(""));
        System.out.println(maxLen("a"));
        System.out.println(maxLen("aa"));
        System.out.println(maxLen("abcddd"));
        System.out.println(maxLen("abcd"));
        System.out.println(maxLen("aabbba"));
    }

    public static String maxLen(String input) {
        // Avoid NPEs
        if (input == null) {
            return null;
        }
        int maxLen = 0;
        int tempLen = 0;
        char prevChar = 0;
        char c = 0;
        char repeatChar = 0;
        for (int i = 0; i < input.length(); i++) {
            c = input.charAt(i);
            if (c == prevChar) {
                tempLen++;
                if (tempLen > maxLen)
                    repeatChar = c;
            } else {
                maxLen = (tempLen > maxLen) ? tempLen : maxLen;
                prevChar = c;
                tempLen = 1;
            }
        }
        maxLen = (tempLen > maxLen) ? tempLen : maxLen;
        if (maxLen == 0 || maxLen == 1)
            return "no sequence found";
        else {
            String str = "";
            for (int i = 1; i <= maxLen; i++)
                str += String.valueOf(repeatChar);
            return str;
        }
    }
}

This will pass all the test cases.

0
source

Source: https://habr.com/ru/post/1526697/


All Articles