-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstoccurence2.java
More file actions
34 lines (31 loc) · 849 Bytes
/
Copy pathFirstoccurence2.java
File metadata and controls
34 lines (31 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*Given a string S of lowercase English letters, the task is to find the index of the first non- repeating character. If there is no such A character, return -1.*/
public class Firstoccurence2
{
public static void main(String args[])
{
String s="aaabbbccc";
char[] arr=s.toCharArray();
int result=Firstoccurence(arr);
System.out.println(result);
}
public static int Firstoccurence(char[] arr)
{
for (int i=0;i<arr.length;i++)
{
int count=0;
for(int j=0;j<arr.length;j++)
{
if(arr[i]==arr[j])
{
count++;
}
}
if(count==1)
{
return i;
}
}
return -1;
}
}
//TIMECOMPLEXCITY=O(n)