Skip to content

Latest commit

 

History

History
57 lines (50 loc) · 2.42 KB

Check_Palindrome.md

File metadata and controls

57 lines (50 loc) · 2.42 KB

🔷 Check Palindrome Challenge:

Challenge description

Given the string, check if it is a palindrome.

Example

  • For inputString = "aabaa", the output should be
    solution(inputString) = true;
  • For inputString = "abac", the output should be
    solution(inputString) = false;
  • For inputString = "a", the output should be
    solution(inputString) = true.

Input/Output

  • [execution time limit] 3 seconds (java)

  • [memory limit] 1 GB

  • [input] string inputString

    A non-empty string consisting of lowercase characters.

    Guaranteed constraints:
    1 ≤ inputString.length ≤ 105.

  • [output] boolean

    true if inputString is a palindrome, false otherwise.

[Java] Syntax Tips

// Prints help message to the console
// Returns a string
// 
// Globals declared here will cause a compilation error,
// declare variables inside the function instead!
String helloWorld(String name) {
    System.out.println("This prints to the console when you Run Tests");
    return "Hello, " + name;
}

Solutions:

  • JS solution

    function solution(word){
    for (let i = 0, j = word.length - 1; i<word.length/2; i++, j-- )
    if(word[i] != word[j])
    return false
    return true
    }
    console.log('"aabaa" is' + (solution("aabaa")?' ': ' not ') + 'a palindrome')
    console.log('"a" is' + (solution("a")?' ': ' not') + ' a palindrome')
    console.log('"abac" is ' + (solution("abac")? '': 'not') + ' a palindrome')
    JS Execution

  • Java solution

    public class Check_palindrome{
    public static boolean solution (String inputString){
    for(int left = 0, right = inputString.length()-1; left<inputString.length()/2; left++, right--)
    if(inputString.charAt(left) != inputString.charAt(right))
    return false;
    return true;
    }
    public static void main(String[] args){
    System.out.println(" 'aabaa' is " + (solution("aabaa")? "": "not") + " a palindrome.");
    System.out.println(" 'abac' is " + (solution("abac")? " a palindrome": "not a palindrome."));
    System.out.println(" 'a' is a palindrome? " + solution("a") );
    }
    }
    Java Execution