英文:
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
不返回-1
或char
,你可以尝试返回字符串的索引或-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 < str.length(); i++) {
int v = str.charAt(i)-'a';
cnt[v]++;
if(cnt[v] > 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("絵文字も処理できる😊😊😁😂")));
输出
😊
英文:
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 < max; p = n, ++i)
if ((n = codePoints[i]) == p)
return p;
return -1;
}
and
System.out.println(Character.toString(first("絵文字も処理できる😊😊😁😂")));
output
😊
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论