-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome string in array.java
More file actions
56 lines (48 loc) · 1003 Bytes
/
Palindrome string in array.java
File metadata and controls
56 lines (48 loc) · 1003 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution {
public String firstPalindrome(String[] words) {
String res="";
for(String s : words)
{
if(isPalindrome(s))
{
res=s;
break;
}
}
return res;
}
static boolean isPalindrome(String s)
{
boolean flag=false;
int i=0;
int j=s.length()-1;
if(s.length()==1)
flag=true;
while(i<j)
{
if(s.charAt(i)==s.charAt(j))
{
flag=true;
i++;
j--;
}
else
{
flag=false;
break;
}
}
return flag;
}
}
//Stringbuilder approach
class Solution {
public String firstPalindrome(String[] words) {
for(String i : words){
StringBuilder str = new StringBuilder(i);
str.reverse();
if(i.equals(str.toString())) return i;
}
return "";
}
}