File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ //Palindrome or not
2
+ let word = prompt ( "Enter word to check palindrome:" ) . toLowerCase ( ) ;
3
+ let rev = ""
4
+ for ( let i = word . length - 1 ; i >= 0 ; i -- ) {
5
+ rev += word [ i ]
6
+ }
7
+
8
+ rev === word ? console . log ( "its a palindrome" ) :console . log ( "its not a palindrome" )
9
+
10
+ // Easiest version
11
+ let isPalindrome = gon === gon . split ( "" ) . reverse ( ) . join ( "" ) ;
12
+ console . log ( isPalindrome ? `${ gon } is a palindrome` :`${ gon } is not a palindrome` )
13
+
14
+ function longestPalindrome ( s ) {
15
+ let longest = '' ;
16
+
17
+ for ( let i = 0 ; i < s . length ; i ++ ) {
18
+ for ( let j = i ; j < s . length ; j ++ ) {
19
+ let substring = s . substring ( i , j + 1 ) ;
20
+ if ( substring === substring . split ( '' ) . reverse ( ) . join ( '' ) && substring . length > longest . length ) {
21
+ longest = substring ;
22
+ }
23
+ }
24
+ }
25
+
26
+ return longest ;
27
+ }
28
+ console . log ( longestPalindrome ( "babad" ) ) ; // Output: "bab" or "aba" (both are valid)
29
+ console . log ( longestPalindrome ( "cbbcabc" ) ) ; // Output: "cbbc"
30
+ console . log ( longestPalindrome ( "a" ) ) ; // Output: "a"
31
+ console . log ( longestPalindrome ( "ac" ) ) ; // Output: "a"
You can’t perform that action at this time.
0 commit comments