Efficiently find first repeated character in a string without using any additional data structure in one traversal

huangapple go评论61阅读模式
英文:

Efficiently find first repeated character in a string without using any additional data structure in one traversal

问题

def find_first_repeated_character(s):
    seen_chars = set()
    for ch in s:
        if ch in seen_chars:
            return ch
        seen_chars.add(ch)
    return -1

You can use this Python function find_first_repeated_character to find the first repeated character in a given string s. It will return the first repeated character or -1 if there are no repeated characters.

Please integrate this function into your existing code structure for the desired functionality.

英文:

Given a string S. The task is to find the first repeated character in it. We need to find the character that occurs more than once and whose index of second occurrence is smallest. S contains only lowercase letters.

It is giving wrong output for
input-'crg'
output-?
expected -'-1'

Here is my code-

private static Character first(String s)
	{
	    String str ="";
	    char c =(char)-1;
	   // int flag=-1;
	    char c1='
private static Character first(String s)
{
String str ="";
char c =(char)-1;
// int flag=-1;
char c1='\0';
for(int i=0;i<s.length();i++)
{
char ch = s.charAt(i);
if(str.indexOf(ch)==-1)
str+=ch;
else{
c1=ch;
break;
}
}
if(s.equals(str))
return c;
else
return c1;
}
'; for(int i=0;i<s.length();i++) { char ch = s.charAt(i); if(str.indexOf(ch)==-1) str+=ch; else{ c1=ch; break; } } if(s.equals(str)) return c; else return c1; }

答案1

得分: 1

(char)-1\uffff 相同,它将始终被打印为 ?,因为 \uffff 不是有效的Unicode字符。

英文:

(char)-1 is the same as \uffff, it will always be printed as ? because \uffff is not a valid unicode character.

答案2

得分: 0

不返回-1char,你可以尝试返回字符串的索引或-1,因为-1不是有效的Unicode字符。

以下是一个更好的解决方案,使用整数数组来计算字符串中的出现次数,如果找到第二次出现,它将返回索引,否则返回-1

static int check(String str) {
    int[] cnt = new int[26];
    for (int i = 0; i < str.length(); i++) {
        int v = str.charAt(i) - 'a';
        cnt[v]++;
        if (cnt[v] > 1) {
            return i;
        }
    }
    return -1;
}
英文:

Rather trying return -1 or char, you can try to return index of String or -1, since -1 is not a valid Unicode character.

Here is a better solution using an int array for counting occurrence in the string, if found 2nd occurrence it will return the index or return -1.

  static int check(String str){
    int[] cnt= new int[26];
    for(int i = 0; i &lt; str.length(); i++) {
      int v = str.charAt(i)-&#39;a&#39;;
      cnt[v]++;
      if(cnt[v] &gt; 1) {
        return i;
      }
    }
    return -1;
  }

答案3

得分: 0

尝试这个。

private static int first(String s) {
    int[] codePoints = s.codePoints().toArray();
    int max = codePoints.length;
    for (int i = 0, p = 0, n = 0; i < max; p = n, ++i)
        if ((n = codePoints[i]) == p)
            return p;
    return -1;
}


System.out.println(Character.toString(first("絵文字も処理できる&#128522;&#128522;&#128513;&#128514;")));

输出

&#128522;
英文:

Try this.

private static int first(String s) {
    int[] codePoints = s.codePoints().toArray();
    int max = codePoints.length;
    for (int i = 0, p = 0, n = 0; i &lt; max; p = n, ++i)
        if ((n = codePoints[i]) == p)
            return p;
    return -1;
}

and

System.out.println(Character.toString(first(&quot;絵文字も処理できる&#128522;&#128522;&#128513;&#128514;&quot;)));

output

&#128522;

huangapple
  • 本文由 发表于 2020年8月7日 15:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63297572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定